gpt4 book ai didi

java - 安卓 : ArrayList with Bitmap from assets

转载 作者:行者123 更新时间:2023-12-02 04:14:58 34 4
gpt4 key购买 nike

我一直在尝试从 Assets 目录中的图像中列出一个列表,这是我的代码

ArrayList<Bitmap> list = new ArrayList<>();

try{
String[] PathFiles = assetManager.list("img");
for (int i = 0; i < PathFiles.length; i++) {
InputStream is = assetManager.open("img/" + PathFiles[i]);
Bitmap bitmap = BitmapFactory.decodeStream(is);
list.add(i, bitmap);
}
}
catch(IOException e)
{
e.printStackTrace();
}

但是当执行此代码时,我的应用程序就会崩溃。会有什么问题呢?

应用程序的logcat:

10-29 18:19:06.712  18272-18272/com.example.mohammed.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mohammed.myapplication, PID: 18272
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammed.myapplication/com.example.mohammed.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.content.res.AssetManager.list(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.content.res.AssetManager.list(java.lang.String)' on a null object reference
at com.example.mohammed.myapplication.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
            at android.app.ActivityThread.access$900(ActivityThread.java:181)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6117)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

最佳答案

我想说你在这里得到一个NullPointerException:

list.add(i, bitmap);

您没有创建ArrayList:

ArrayList<Bitmap> list;

尝试一下:

ArrayList<Bitmap> list = new ArrayList<>;

关于java - 安卓 : ArrayList with Bitmap from assets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418957/

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