gpt4 book ai didi

android - 执行调用 Activity onActivityResult 的 fragment onActivityResult 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:51 29 4
gpt4 key购买 nike

在我的 fragment 中,我已经启动了用于照片捕获的 startActivityforresult Intent 。我已经覆盖了 fragment 类中的 onActivityResult 回调方法。为了其他目的,我在主要 Activity 中实现了 onActivityResult 回调。我的问题是 fragment onActivityResult 执行后调用 Activity onActivityResult 方法并返回空指针异常。 fragment 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, 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);
}
}
}

ActivityResult 上的 Activity

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

如何只调用fragment的onactivityresult方法?

我的日志

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

最佳答案

如果您的 Activity 中定义了 onActivityResult,则无法跳过它并直接转到 Fragment。但是,如果 Activity 不知道如何处理它,您可以将它重定向到 Fragment。使用唯一的请求代码来区分谁处理结果。

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

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

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

希望这对您有所帮助。

关于android - 执行调用 Activity onActivityResult 的 fragment onActivityResult 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434617/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com