我使用以下代码让用户从他的图库中选择图像。但是在选择一个 img 后,应用程序崩溃了。
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
这是崩溃的日志:
04-01 00:58:53.210: E/AndroidRuntime(26970): FATAL EXCEPTION: main
04-01 00:58:53.210: E/AndroidRuntime(26970): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/21931 flg=0x1 }} to activity {de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.AddNewMission}: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread.deliverResults(ActivityThread.java:3310)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3353)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread.access$1100(ActivityThread.java:145)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.os.Looper.loop(Looper.java:137)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread.main(ActivityThread.java:4978)
04-01 00:58:53.210: E/AndroidRuntime(26970): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970): at java.lang.reflect.Method.invoke(Method.java:511)
04-01 00:58:53.210: E/AndroidRuntime(26970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-01 00:58:53.210: E/AndroidRuntime(26970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-01 00:58:53.210: E/AndroidRuntime(26970): at dalvik.system.NativeStart.main(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970): Caused by: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970): at de.arvidg.exampleactionbartabs.AddNewMission.onActivityResult(AddNewMission.java:131)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
04-01 00:58:53.210: E/AndroidRuntime(26970): at android.app.ActivityThread.deliverResults(ActivityThread.java:3306)
04-01 00:58:53.210: E/AndroidRuntime(26970): ... 11 more
这是发生异常的onActivityResult。
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.chosenpictureview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
filepath = picturePath;
thread.setPathToOurFile(picturePath);
}
}