java.lang.NullPointerException: 尝试在空对象引用上调用接口方法 'boolean android.database.Cursor.moveToFirst()'
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
我正在创建一个应用程序来列出我 phone 中的所有图像,并且我正在使用媒体内容提供商来执行此操作。
我的问题是我的应用程序总是在 运行 任何地方出现此错误。
08-28 00:31:27.001 16706-16706/com.chill.leoj.burp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chill.leoj.burp, PID: 16706
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chill.leoj.burp/com.chill.leoj.burp.Photos}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at com.chill.leoj.burp.Photos.onCreate(Photos.java:159)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Photos.java:159
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
请你帮我解决这个问题。提前致谢!
这是我的代码:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uri1 = MediaStore.Images.Thumbnails.getContentUri("external");
String[] thumbColumns = { MediaStore.Images.Thumbnails.DATA,
MediaStore.Images.Thumbnails.IMAGE_ID };
String[] mediaColumns = { MediaStore.Images.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.MIME_TYPE };
Cursor cursor = getContentResolver().query(uri,
mediaColumns, null, null, null);
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = getContentResolver().query(uri1, thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
Log.v("", newVVI.title);
newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
Log.v("", newVVI.mimeType);
ImageRows.add(newVVI);
} while (cursor.moveToNext());
cursor.close();
ImageAdapter = new ImageGalleryAdapter(Photos.this, ImageRows);
}
listView.setAdapter(new ImageGalleryAdapter(this, ImageRows));
编辑:
我忘了告诉我我也尝试了写在 this link 上的内容并自己调试但没有真正发生,我不知道我是否做对了。
试试这个
if (thumbCursor.moveToFirst()!=null && (thumbCursor.moveToFirst())
在
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
和
if (Cursor.moveToFirst()!=null && (Cursor.moveToFirst())
在
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = getContentResolver().query(uri1, thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
我正在创建一个应用程序来列出我 phone 中的所有图像,并且我正在使用媒体内容提供商来执行此操作。 我的问题是我的应用程序总是在 运行 任何地方出现此错误。
08-28 00:31:27.001 16706-16706/com.chill.leoj.burp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chill.leoj.burp, PID: 16706
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chill.leoj.burp/com.chill.leoj.burp.Photos}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
at com.chill.leoj.burp.Photos.onCreate(Photos.java:159)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Photos.java:159
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
请你帮我解决这个问题。提前致谢!
这是我的代码:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uri1 = MediaStore.Images.Thumbnails.getContentUri("external");
String[] thumbColumns = { MediaStore.Images.Thumbnails.DATA,
MediaStore.Images.Thumbnails.IMAGE_ID };
String[] mediaColumns = { MediaStore.Images.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.MIME_TYPE };
Cursor cursor = getContentResolver().query(uri,
mediaColumns, null, null, null);
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = getContentResolver().query(uri1, thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
Log.v("", newVVI.title);
newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
Log.v("", newVVI.mimeType);
ImageRows.add(newVVI);
} while (cursor.moveToNext());
cursor.close();
ImageAdapter = new ImageGalleryAdapter(Photos.this, ImageRows);
}
listView.setAdapter(new ImageGalleryAdapter(this, ImageRows));
编辑: 我忘了告诉我我也尝试了写在 this link 上的内容并自己调试但没有真正发生,我不知道我是否做对了。
试试这个
if (thumbCursor.moveToFirst()!=null && (thumbCursor.moveToFirst())
在
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
和
if (Cursor.moveToFirst()!=null && (Cursor.moveToFirst())
在
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = getContentResolver().query(uri1, thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}