I’m facing this null object error even though I have checked the “setContentView(R.layout.activity_main)” and by commenting the onClick methods the app works fine.
The idea is simple, all I want is to get the image into a intent then process it to identify it
Here is the code:
package com.example.letmeguess;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.image.TensorImage;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.letmeguess.ml.MobilenetV110224Quant;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
Button sB, pB, cB;
TextView result;
ImageView imageView;
Bitmap bm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sB =(android.widget.Button) findViewById(R.id.selectB);
pB =(android.widget.Button) findViewById(R.id.PredicteB);
cB =(android.widget.Button) findViewById(R.id.captureB);
result = findViewById(R.id.result);
imageView = findViewById(R.id.imageView);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
sB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_GET_CONTENT);
intent1.setType("image/*");
startActivityForResult(intent1,10);
}
});
cB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent2, 12);
}
});
// permission
getPermission();
int cnt = 0;
String[] lables = new String[1001];
try {
BufferedReader br= new BufferedReader(new InputStreamReader(getAssets().open("labels.txt")));
String line = br.readLine();
while (line != null){
lables[cnt] = line;
cnt++;
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
pB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
MobilenetV110224Quant model = MobilenetV110224Quant.newInstance(MainActivity.this);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.UINT8);
bm = Bitmap.createScaledBitmap(bm, 224, 224, true);
inputFeature0.loadBuffer(TensorImage.fromBitmap(bm).getBuffer());
// Runs model inference and gets result.
MobilenetV110224Quant.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
result.setText(lables[getMax(outputFeature0.getFloatArray())]+" ");
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}
}
});
}
int getMax(float[] arr){
int max = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] > arr[max]) max = i;
}
return max;
}
void getPermission(){
if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},11);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == 11){
if(grantResults.length > 0){
if(grantResults[0] != PackageManager.PERMISSION_GRANTED){
this.getPermission();
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode==10){
if(data!=null){
try {
Uri uri = data.getData();
bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
imageView.setImageBitmap(bm);
}catch (IOException e){
e.printStackTrace();
}
}
}
else if(requestCode == 12){
bm = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bm);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
and the layout.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Image Classify"
android:textAlignment="center"
android:textSize="30dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/selectB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Select Image" />
<Button
android:id="@+id/captureB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/selectB"
android:text="Capture" />
<Button
android:id="@+id/PredicteB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@id/captureB"
android:layout_centerHorizontal="true"
android:text="Predicte" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="Result:"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_below="@id/PredicteB"
android:textSize="20dp" />
</RelativeLayout>
commenting the onClick makes the app works but of course that is not the solution
New contributor
Nier Yorha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.