0

I have a FragmentActivity which contains three Fragments. In Fragment No. 3, I have an ImageView where on click of that ImageView I want pick up image from gallery and set to that ImageView but when I try to do this, I get following exception and app gets crashed.

Code inside 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);

 }
}

Exception

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

When I run same code for an Activity, it works fine but gets crashed for FragmentActivity & Fragment.

halfer
  • 19,824
  • 17
  • 99
  • 186
VVB
  • 7,363
  • 7
  • 49
  • 83
  • Post your code please.. – Skizo-ozᴉʞS ツ Mar 10 '15 at 09:56
  • As the stack trace clearly indicates: something is `null` on line 255 in `SubHomeScreenActivity`. – MH. Mar 10 '15 at 10:01
  • Try to replace your Cursor for this one : `Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null); ` To add the image you should use something like `img_user.setImageBitmap(BitmapFactory.decodeFile(picturePath));` – Skizo-ozᴉʞS ツ Mar 10 '15 at 10:03
  • Thanks for reply. But I have already done it. – VVB Mar 10 '15 at 10:06
  • @Skizo It gets crashed when I try to pick up image from gallery – VVB Mar 10 '15 at 10:11
  • @MH. onActivityResult of SubHomeScreenActivity is the line number 255 – VVB Mar 10 '15 at 10:11
  • Then you've probably made a change since the last time you generated the stack trace. Just look at the stack trace: `Caused by: java.lang.NullPointerException (...) at com.ts.android.smart.checkin.activity.SubHomeScreenActivity.onActivityResult(SubHomeScreenActivity.java:255)`. I'd like to suggest to generate a new one so you can pinpoint the exact/correct location. – MH. Mar 10 '15 at 10:22

0 Answers0