简体   繁体   中英

Android - NPE in onActivityResult() of Fragment

I am calling the Camera using the following code snippet:

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                    getActivity().startActivityForResult(intent, INSTANCE_CAMERA);

onActivity Result my code snippet is :

{
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == active.RESULT_OK) {
            if (requestCode == INSTANCE_CAMERA) {

//            Log.d(this.getTag(), noMediaFile.getAbsolutePath());
//            Bitmap bitmImage = BitmapFactory.decodeFile(noMediaFile.getAbsolutePath());
                Bitmap bitmImage = null;
                bitmImage = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream captureCardImage = new ByteArrayOutputStream();
                bitmImage.compress(Bitmap.CompressFormat.PNG, 100,
                        captureCardImage);
                byteValue = captureCardImage.toByteArray();
                String imageConvertString = Base64.encodeToString(byteValue,
                        Base64.DEFAULT);
                // Issue is this the imageView
                ImageView imvPicture = (ImageView) active.findViewById(R.id.imvPicture);
                Log.d(TAG, bitmImage + "  My Fragment Tag");
                imvPicture.setImageBitmap(bitmImage);
            }
        }
    }

But the ImageView is giving me null pointer

my Xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
   >


    <ImageView
        android:id="@+id/imvPicture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/picture_snapshot" />

    <Button
        android:id="@+id/btnNewEntry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/shady_yellow"
        android:text="@string/title"
        android:textStyle="bold"
        android:textColor="@android:color/black" />
</FrameLayout>

I am getting Null pointer exception on ImageView of onActivityResult

Log Cat Error:

Caused by:

java.lang.NullPointerException
            at com.***.*****.com.**.onActivityResult(TakeNoteFragment.java:150)
            at com.texticky.app.activities.MainActivity.onActivityResult(MainActivity.java:65)
            at android.app.Activity.dispatchActivityResult(Activity.java:5390)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
            at android.app.ActivityThread.access$1200(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method) 

I believe you are calling onActivityResult() in your Fragment class. You need to call it in your Activity class because you have invoked it as getActivity().startActivityForResult(intent, INSTANCE_CAMERA); .

Try this: shift the entire onActivityResult() method to your Activity class, and replace the line:

ImageView imvPicture = (ImageView) getView().findViewById(R.id.imvPicture);

with

ImageView imvPicture = (ImageView) findViewById(R.id.imvPicture);

Also replace:

resultCode == active.RESULT_OK

with

resultCode == this.RESULT_OK

Try this..

getView()

Change this..

ImageView imvPicture = (ImageView) active.findViewById(R.id.imvPicture);

to

ImageView imvPicture = (ImageView) getView().findViewById(R.id.imvPicture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM