我正在尝试使用项目列表完成我的第一个示例应用程序。我的目的是为列表中的每个项目拥有自己的图像,我在stackoverflow上找到了以下代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
main_datasource.pic.setPicturePath(picturePath);
}
}
效果很好,我只能选择一次图像。 当我尝试第二次选择时会弹出以下错误:
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime FATAL EXCEPTION: main
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/2 }} to activity {example.CustomAdapter/example.CustomAdapter.MainActivity}: java.lang.NullPointerException
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.os.Looper.loop(Looper.java:123)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:3683)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:507)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime Caused by: java.lang.NullPointerException
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at example.CustomAdapter.MainActivity.onActivityResult(MainActivity.java:303)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
11:14:41.472 1212 example.CustomAdapter ERROR AndroidRuntime ... 11 more
由于我是Android中的新人,您可以帮助我在我能够选择图像时更新此代码。如果有人会解释我这段代码是如何工作的,那也很有用。