Question

I have a Fragment that allows to take pictures. Some of the fragments have a regular picture, and an extra picture. Meaning that I use two different RESULT CODES for which picture I am taking. The error I get is only when I take use the RESULT_PICTURE_EXTRA code and it seems random.

Why am I getting this error when I use the REQUEST_PICTURE_EXTRA? Also, if it helps, I am showing this picture in a PopupWindow and it doesn't show up.

Fragment Code

private static final int REQUEST_PICTURE = 1;
private static final int REQUEST_PICTURE_EXTRA = 2;

private void showExtraPicture() {
    ImageView extraImage = (ImageView) view.findViewById(R.id.extra_image);

    if (step.getIsReqPictureFinished()) {
        try {
            FileInputStream fis = getActivity().openFileInput(step.getExtraImageFilename());
            Bitmap imgFromFile = BitmapFactory.decodeStream(fis);
            fis.close();
            extraImage.setImageBitmap(imgFromFile);
            extraImage.invalidate();
        } 
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_PICTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");

        ImageHandler imageHandler = new ImageHandler(getActivity());
        String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), false);
        step.setImageFilename(filename);

        finishStep();
        showResult();
        checkIfAllFinished();
    }

    if (requestCode == REQUEST_PICTURE_EXTRA && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");

        ImageHandler imageHandler = new ImageHandler(getActivity());
        String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), true);
        step.setExtraImageFilename(filename);

        if (step.getReqPicture()) { step.setIsReqPictureFinished(true); }
        showExtraPicture();
        checkIfAllFinished();
    }
}

Stack Trace

01-31 14:42:54.192: E/AndroidRuntime(21536): FATAL EXCEPTION: main
01-31 14:42:54.192: E/AndroidRuntime(21536): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=2, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3504 (has extras) }} to activity {com.medusa.checkit.android/com.medusa.checkit.android.StepActivity}: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.access$1200(ActivityThread.java:143)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.os.Looper.loop(Looper.java:137)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.main(ActivityThread.java:4950)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at dalvik.system.NativeStart.main(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536): Caused by: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.medusa.checkit.android.StepFragment.showExtraPicture(StepFragment.java:388)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at com.medusa.checkit.android.StepFragment.onActivityResult(StepFragment.java:547)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.Activity.dispatchActivityResult(Activity.java:5367)
01-31 14:42:54.192: E/AndroidRuntime(21536):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
01-31 14:42:54.192: E/AndroidRuntime(21536):    ... 11 more
Was it helpful?

Solution

Read your error message: a NullPointerException occurs inside showExtraPicture(). That's why it only happens with REQUEST_PICTURE_EXTRA code.

You need to find what's null and why. This is on line 388, so only you know, but I'd bet on view being null.

Whether it is or not anyway, you shouldn't hold onto your created root View (assuming this is what view reference here is). Just find your views from the attached Activity each time you need it:

getActivity().findViewById(R.id.extra_image);

or from the fragment's root view works too if R.id.extra_image is held inside it:

getView().findViewById(R.id.extra_image);

And, as a side note, you should close resources in finally: see the example in FileInputStream documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top