Android - 片段的onActivityResult()中的NPE

时间:2014-06-04 12:46:56

标签: android android-fragments android-activity android-camera android-imageview

我使用以下代码片段调用相机:

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

onActivity结果我的代码段是:

{
        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);
            }
        }
    }

但是ImageView给了我空指针

我的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>

我在onActivityResult的

的ImageView上获得Null指针异常

Log Cat错误:

引起:

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) 

2 个答案:

答案 0 :(得分:1)

我相信您在onActivityResult()课程中致电Fragment。您需要在Activity课程中调用它,因为您已将其作为getActivity().startActivityForResult(intent, INSTANCE_CAMERA);调用。

试试这个:将整个onActivityResult()方法转移到Activity类,然后替换该行:

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

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

同时替换:

resultCode == active.RESULT_OK

resultCode == this.RESULT_OK

答案 1 :(得分:0)

试试这个..

getView()

改变这个..

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

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