I have a simple Android app with 2 activities and a base activity where I store a global array of images.
The ImageGrid activity displays a grid of images that the user can touch/click to display a larger version of that image.
When clicked, ImageGrid sends the selected index of the image that was touched to the selectedImage activity.
However, everytime it does that, I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at SelectedImage.onCreate(SelectedImage.java:19)
Line 19 is this:
int arrNum = arrPosition;
I've stepped through the debugger, and I do see the index position getting passed and set, so I'm at a loss as to why I'm getting a NullPointerException.
Here is the BaseActivity:
public class BaseActivity extends AppCompatActivity {
Integer[] ApocImages = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16};
}
ImageGrid:
public class ImageGrid extends BaseActivity {
ImageView gridThumbnail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_grid);
GridView grid = findViewById(R.id.gridView);
final ImageView selectedImage = findViewById(R.id.imgLarge);
grid.setAdapter(new ImageAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(ImageGrid.this, SelectedImage.class);
int arrPos = position;
myIntent.putExtra("selectedImage", arrPos);
startActivity(myIntent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context c) {
context = c;
}
public int getCount() {
return ApocImages.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
//gets the view for each item in the array
public View getView(int position, View contentView, ViewGroup parent) {
// populate gridview with imageview of each photo in array
gridThumbnail = new ImageView(context);
gridThumbnail.setImageResource(ApocImages[position]);
gridThumbnail.setScaleType(ImageView.ScaleType.FIT_XY);
gridThumbnail.setLayoutParams(new GridView.LayoutParams(330, 300));
return gridThumbnail;
}
}
}
SelectedImage:
public class SelectedImage extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context appContext = getApplicationContext();
final ImageView selectedImage = findViewById(R.id.imgLarge);
Intent intent = getIntent();
int arrPosition = intent.getIntExtra("selectedImage", 1);
int arrNum = arrPosition;
selectedImage.setImageResource(ApocImages[arrNum]);
selectedImage.setClickable(true);
selectedImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(new Intent(SelectedImage.this, ImageGrid.class));
finish();
}
});
}
}