【问题标题】:Null Pointer Exception while putting Image In Bitmap将图像放入位图中时出现空指针异常
【发布时间】:2014-03-07 17:14:59
【问题描述】:

我在将图像放入位图中时面临 NullpointerExcetion。我正在从 SD 卡获取图像。

        Uri path = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    imageFile = new File(getRealPathFromURI(path));
    if(imageFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imageView1);
        myImage.setImageBitmap(myBitmap);

    }

【问题讨论】:

  • 由于您的代码不检查空值,您能告诉我们 NullPointerException 发生在哪里吗?
  • @shkschneider...最后一行。 myImage.setImageBitmap(myBitmap);
  • 总是发布完整的堆栈跟踪,谢谢。
  • 测试 myImage 是否为 null 或 myBitmap 是否为 null,此时可能有很多事情。发布更多信息(stacktrace +1)

标签: android bitmap imageview


【解决方案1】:

猜测:myImagenull。检查您导入的R 是否正确。

【讨论】:

  • 不客气,别忘了标记为已接受的答案。
【解决方案2】:

如果您在 Activity 中,请检查 imageView1 是否在您传递给 setContentView 方法的布局中。

如果您正在使用片段或以编程方式扩展了布局,请改用view.findViewBy(R.id.imageView1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多