2

我有一种情况,我需要使用用户外部存储目录中的图像填充图像视图;我一直在使用位图工厂解码文件,然后相应地设置图像视图位图,但我一直遇到空指针异常。

String path = Environment.getExternalStorageDirectory()+ "/Pictures/test.jpg";
File imgFile = new File(path);

    if(imgFile.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ImageView imgView = (ImageView)findViewById(R.id.imageView2);
        imgView.setImageBitmap(myBitmap);
    } else {
        Log.d("Ciaren", "File doesn't exist");
    }

我直接在 OnCreate 中运行它,并且图像视图应该是全屏的,但我终其一生都无法弄清楚是什么抛出了一个空对象,因为我已经逐步完成并且一切看起来都很好,空setImageBitmap() 时抛出指针;方法被调用。

4

1 回答 1

1

The error is thrown in imgView.setImageBitmap(myBitmap); and thus imageView is null. As a result your imageView is not found. Make sure the resource id R.id.imageView2 is correct. Sometimes there are problems with eclipse and a Project > Clean can fix that too.

于 2013-11-17T17:18:16.980 回答