This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                        
                            findViewById returns null
                                
                                    (3个答案)
                                
                        
                2年前关闭。
            
        

我有一个简单的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();
            }
        });
    }
}

最佳答案

SelectedImage(我建议将其命名为SelectedImageActivity)中,您拨打电话final ImageView selectedImage = findViewById(R.id.imgLarge);,但从未指定setContentView(R.layout.activity_image_grid);。您必须设置一个内容视图,其中包含id:R.id.imgLarge,以便该活动能够找到它。

10-06 05:55