android – 片段onActivityResult方法执行调用活动onActivityResult

前端之家收集整理的这篇文章主要介绍了android – 片段onActivityResult方法执行调用活动onActivityResult前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的片段中,我已经启动了startActivityforresult意图进行照片捕获.我已经在片段类中重写了onActivityResult回调方法.我已经在主要活动中实现了onActivityResult回调以实现其他一些意图.在执行调用活动onActivityResult方法并返回空指针异常后,我的问题是片段onActivityResult.
片段onactivityresult方法
@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);
                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);
        }
    }
}

活动onActivityResult

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

如何只调用片段onactivityresult方法

我的Logcat

将结果ResultInfo {who = null,request = 1,result = -1,data = Intent {act = inline-data dat = content:// media / external / images / media / 222(has extras)}}传递给活动失败{com.mobiotics.tvbuddydemo / com.mobiotics.tvbuddydemo.TVBuddyMainActivity}:java.lang.NullPointerException

解决方法

如果您在Activity中定义了onActivityResult,则不能跳过它并直接转到Fragment.但是,如果Activity不知道如何处理它,您可以将其重定向到Fragment.使用唯一的requestCodes来区分处理结果的人员.
public void onActivityResult(int requestCode,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,data);
        fragment2.onActivityResult(requestCode,data);
        ...
        super.onActivityResult(requestCode,data);
    }
}

注意:确保从片段中调用getActivity().startActivityForResult()而不是简单地使用this.startActivityForResult()

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

希望这可以帮助.

猜你在找的Android相关文章