Fragment onActivityResult method on executing call

2019-03-20 12:06发布

In my fragment I have started startActivityforresult intent for photo capture.I have overridden onActivityResult callback method in fragment class. I have implemented onActivityResult callback in main activity for some other intent. My problem is fragment onActivityResult after execution calls activities onActivityResult method and returns null pointer exception. Fragment onactivityresult method

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) 
    {
        prof_bitmap = null;
        if (requestCode == 0) 
        {
            Log.e("" ,"entered activity Result Code 0");
            Uri photoUri = data.getData();
            if (photoUri != null) 
            {
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getActivity().getContentResolver().query(
                        photoUri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();
                Log.e("" ,"File Path" +filePath);
                prof_bitmap = setImage(filePath);
            }
        }
        if (requestCode == 1) 
        {
            Log.e("" ,"entered activity Result Code 1");
            Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
            prof_bitmap = bitmap;
            Log.e("" ,"entered activity Result Code 1"+bitmap);
            profile_pic.setImageBitmap(bitmap);
        }
    }
}

Activity onActivityResult

  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    Log.e("" ,"called onActivityResult in main");
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

how to call only fragment onactivityresult method?

MY Logcat

Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/222 (has extras) }} to activity {com.mobiotics.tvbuddydemo/com.mobiotics.tvbuddydemo.TVBuddyMainActivity}: java.lang.NullPointerException

5条回答
爷的心禁止访问
2楼-- · 2019-03-20 12:27

The onActivityResult in Activity will be called first, you should not remove processing the result in Activity but do what you want via checking the requestCode.

查看更多
Root(大扎)
3楼-- · 2019-03-20 12:29

If you have onActivityResult defined in your Activity, you can't skip it and go directly to the Fragment. You can however redirect it to the Fragment if the Activity does not know how to handle it. Use unique requestCodes to differentiate between who handles the result.

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    boolean processed = true;

    if (resultCode == Activity.RESULT_OK) 
    {
        if (requestCode == 0) {
            // Something
        } else if (requestCode == 1) {
            // Something
        } else {
            processed = false;
        }
    } else { // Error
        if (requestCode == 0) {
            // Handle error 
        } else {
            processed = false;
        }
    }

    if (!processed) {
        fragment1.onActivityResult(requestCode, resultCode, data);
        fragment2.onActivityResult(requestCode, resultCode, data);
        ...
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Note: Make sure to call getActivity().startActivityForResult() from your fragment and not simply using this.startActivityForResult()

public void something(Intent intent) {
    getActivity().startActivityForResult(intent);
    // or if you are using SherlockActionBar/Support package
    getSupportActivity().startActivityForResult(intent);
}

Hope this helps.

查看更多
萌系小妹纸
4楼-- · 2019-03-20 12:37

Try remove the line super.onActivityResult(requestCode, resultCode, data); in your fragment onActivityResult().

查看更多
不美不萌又怎样
5楼-- · 2019-03-20 12:37

public void onActivityResult is called when the Activity you started with startActivityForResult is finished calling finish()

查看更多
何必那么认真
6楼-- · 2019-03-20 12:40

I aslo face this problem.

Calling of an activity from fragment

((Activity)context).startActivityForResult(photoPickerIntent, 100);

here "context" from FragmentActivity. Now the result comes in FargmentActivity onActivityResult method. to switch this result you have to declare a static variable of fragment class in fragmetn activity class.

static ActivityUploadPhotos activityUploadPhotos;

...
ActivityUploadPhotos f = new ActivityUploadPhotos();
ActivityHome.activityUploadPhotos=f;
......

if(activityUploadPhotos!=null)
{
           activityUploadPhotos.onActivityResult(requestCode, resultCode, data);
}
查看更多
登录 后发表回答