gpt4 book ai didi

java - 为什么 .setImageBitmap() 不将图像分配给 ImageView 对象?

转载 作者:行者123 更新时间:2023-12-01 09:54:38 26 4
gpt4 key购买 nike

我有一种方法可以读取外部存储空间中的图像。我想将这些图像存储在 ImageView ArrayList 中。为此,我定义了要搜索的文件夹,并创建了一个位图变量,该变量分配了我想要转换为 ImageView 的图像,但是 .setImageBitmap(saveImage)没有分配给我的 Imageview 对象。

代码如下:

 private ArrayList<ImageView> getData() {

ImageView singleImage = null;
final ArrayList<ImageView> addImage = new ArrayList<>();
File retrieveImages = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "/WordResolver");

for(File countImages : retrieveImages.listFiles()) {

if(countImages.isFile() && (countImages.getName().endsWith(".jpg"))) {

Bitmap saveImage = BitmapFactory.decodeFile(countImages.getAbsolutePath());
singleImage.setImageBitmap(saveImage); //Fails here
addImage.add(singleImage);
}

}

return addImage;
}

这是图像正在被读取并存储在 BitMap 对象中的证据。

1

这是抛出 NPE 错误的堆栈跟踪,即使 ImageView 应该分配有图像。

05-20 03:13:38.220 8641-8641/com.example.aaron.wordresolver E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aaron.wordresolver, PID: 8641 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaron.wordresolver/com.example.aaron.wordresolver.PictureGallery}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at com.example.aaron.wordresolver.PictureGallery.getData(PictureGallery.java:78) at com.example.aaron.wordresolver.PictureGallery.onCreate(PictureGallery.java:45) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

最佳答案

singleImage 未正确初始化,将其指定为 null 是不够的。

OnCreate()中初始化singleImage:

singleImage = (ImageView)findViewById(R.id.image);

删除:

ImageView singleImage = null;

并在初始化后运行getData()

关于java - 为什么 .setImageBitmap() 不将图像分配给 ImageView 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37340291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com