调用Card.addImage(URI)时的NullPointerException(NullPoi

2019-09-28 17:25发布

尝试添加图像时,我得到一个NullPointerException UriCard 。 有什么想法吗? 它是在GDK的错误吗? 这种方法记录 。

相关的代码:

for (CardPresenter cardPresenter : mCardPresenters) {
    Card card = new Card(this);
    card.setText(cardPresenter.getText());
    card.setInfo(cardPresenter.getFooter());

    for (Uri uri : cardPresenter.getImages()) {
        if (uri != null) {
            Log.w(TAG, "ImageUri: " + uri.toString());
            card.addImage(uri); // <!------------------- ERROR
        } else {
            Log.w(TAG, "We got a null imageUri!");
        }
    }

    mCards.add(card);
}

堆栈跟踪:

12-08 12:51:42.211: W/ResultsActivity(10525): ImageUri: /mnt/sdcard/Pictures/BarcodeEye/BarecodeEye_1386528701794.png
12-08 12:51:42.211: W/dalvikvm(10525): threadid=1: thread exiting with uncaught exception (group=0x40db41f8)
12-08 12:51:42.218: E/AndroidRuntime(10525): FATAL EXCEPTION: main
12-08 12:51:42.218: E/AndroidRuntime(10525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.barcodeeye/com.github.barcodeeye.migrated.ResultsActivity}: java.lang.NullPointerException
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.os.Looper.loop(Looper.java:137)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread.main(ActivityThread.java:4424)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at java.lang.reflect.Method.invokeNative(Native Method)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at java.lang.reflect.Method.invoke(Method.java:511)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at dalvik.system.NativeStart.main(Native Method)
12-08 12:51:42.218: E/AndroidRuntime(10525): Caused by: java.lang.NullPointerException
12-08 12:51:42.218: E/AndroidRuntime(10525):    at com.google.android.glass.app.Card.addImage(Card.java:153)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at com.github.barcodeeye.migrated.ResultsActivity.onCreate(ResultsActivity.java:80)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.Activity.performCreate(Activity.java:4470)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-08 12:51:42.218: E/AndroidRuntime(10525):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-08 12:51:42.218: E/AndroidRuntime(10525):    ... 11 more

文件结构:

shell@android:/mnt/sdcard/Pictures/BarcodeEye $ pwd
/mnt/sdcard/Pictures/BarcodeEye
shell@android:/mnt/sdcard/Pictures/BarcodeEye $ ls -al
-rw-rw-r-- root     sdcard_rw    23271 2013-12-08 12:41 BarecodeEye_1386528100581.png
-rw-rw-r-- root     sdcard_rw    23582 2013-12-08 12:43 BarecodeEye_1386528221554.png
-rw-rw-r-- root     sdcard_rw    21952 2013-12-08 12:45 BarecodeEye_1386528300992.png
-rw-rw-r-- root     sdcard_rw    19595 2013-12-08 12:47 BarecodeEye_1386528471400.png
-rw-rw-r-- root     sdcard_rw    22473 2013-12-08 12:51 BarecodeEye_1386528701794.png

Answer 1:

问题是,我并没有界定方案。

我的URI是: /mnt/sdcard/Pictures/BarcodeEye/BarcodeEye_1386528701794.png
应该是: file:///mnt/sdcard/Pictures/BarcodeEye/BarcodeEye_1386528701794.png

关键是要使用: Uri.fromFile

public Uri getImageUri(String name) {
    return Uri.fromFile(new File(getImageAbsolutePath(name)));
}


文章来源: NullPointerException when calling Card.addImage(Uri)