我有一个简单的Android应用程序,包含2个活动和一个基本活动,我存储了一个全局图像数组。
ImageGrid活动显示用户可以触摸/单击以显示该图像的更大版本的图像网格。
单击时,ImageGrid会将所选图像的索引发送到selectedImage活动。
然而,每次它都这样做,我得到这个错误:
java.lang.NullPointerException:尝试调用虚方法'void 空对象上的android.widget.ImageView.setImageResource(int)' 参考
引起:java.lang.NullPointerException:尝试调用虚方法'void 空对象上的android.widget.ImageView.setImageResource(int)' 参考 在SelectedImage.onCreate(SelectedImage.java:19)
第19行是这样的:
int arrNum = arrPosition;
我已经通过了调试器,我确实看到索引位置已经过了并设置了,所以我不知道为什么我得到了一个 NullPointerException 。
以下是 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();
}
});
}
}
答案 0 :(得分:2)
在SelectedImage
(我建议将其命名为SelectedImageActivity
),您拨打电话final ImageView selectedImage = findViewById(R.id.imgLarge);
,但从未指定setContentView(R.layout.activity_image_grid);
。您必须设置一个内容视图,其中包含ID:R.id.imgLarge
,以便该活动能够找到它。
答案 1 :(得分:1)
在onCreate()
中,setContentView()
遗失。