我有一个FragmentActivity
,其中包含三个片段。在片段3中,我有一个ImageView
点击ImageView
我想要从图库中选取图像并设置为ImageView
但是当我尝试这样做时,我得到了以下异常和应用程序崩溃。
Fragment No 3中的代码
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// BitmapFactory.decodeFile(picturePath);
}
}
异常
03-10 15:20:40.508: E/AndroidRuntime(6846): FATAL EXCEPTION: main
03-10 15:20:40.508: E/AndroidRuntime(6846): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196609, result=-1, data=Intent { dat=content://media/external/images/media/5329 (has extras) }} to activity {com.ts.android.smart.checkin/com.ts.android.smart.checkin.activity.SubHomeScreenActivity}: java.lang.NullPointerException
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread.deliverResults(ActivityThread.java:3410)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3453)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread.access$1200(ActivityThread.java:150)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.os.Looper.loop(Looper.java:137)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-10 15:20:40.508: E/AndroidRuntime(6846): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 15:20:40.508: E/AndroidRuntime(6846): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 15:20:40.508: E/AndroidRuntime(6846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-10 15:20:40.508: E/AndroidRuntime(6846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-10 15:20:40.508: E/AndroidRuntime(6846): at dalvik.system.NativeStart.main(Native Method)
03-10 15:20:40.508: E/AndroidRuntime(6846): Caused by: java.lang.NullPointerException
03-10 15:20:40.508: E/AndroidRuntime(6846): at com.ts.android.smart.checkin.activity.SubHomeScreenActivity.onActivityResult(SubHomeScreenActivity.java:255)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.Activity.dispatchActivityResult(Activity.java:5472)
03-10 15:20:40.508: E/AndroidRuntime(6846): at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
03-10 15:20:40.508: E/AndroidRuntime(6846): ... 11 more
当我为Activity运行相同的代码时,它可以正常运行但是会因为FragmentActivity而崩溃。片段。