Android Fragment onactivity结果空指针异常

时间:2015-09-20 05:58:33

标签: java android android-fragments onactivityresult

我是android初学者,我的目标是选择图像并在我的ImageView上显示,我使用下面的代码,但在片段onActivity结果返回空指针异常。

这是我的fragmentA.java

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


    switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == getActivity().RESULT_OK) {
                try {
                    final Uri imageUri = imageReturnedIntent.getData();

                    Log.v("imageUri", imageUri.toString());

                    final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
                    final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    image.setImageBitmap(selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
        default:
            initImage();
            break;
    }
}

我在下面的代码中使用了调用库意图

image.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if( Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT )
            {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent.createChooser(intent,"Select Picture"), SELECT_PHOTO);

            }
            else
            {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            }


        }
    });

这是我的错误报告..

java.lang.RuntimeException: Unable to resume activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException


 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException


 Caused by: java.lang.NullPointerException
        at com.caneda.citycenter.myaccount.FragmentTabProfile.onActivityResult(FragmentTabProfile.java:217)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
        at com.caneda.citycenter.myaccount.AccountMainActivity.onActivityResult(AccountMainActivity.java:102)
        at android.app.Activity.dispatchActivityResult(Activity.java:5305)

1 个答案:

答案 0 :(得分:-1)

实际答案真的很傻,我已经初始化了 基于条件的

if(saveInstaceState == null) { ivUpload  = (ImageView) }

应该超出条件。

这就是ivUpload引起nullPointerException的原因。

非常感谢你的支持。