在我的片段中,我已经启动了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, 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, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.e("" ,"called onActivityResult in main");
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
如何只调用片段onactivityresult方法?
我的Logcat
未能提供结果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
答案 0 :(得分:9)
如果您在Activity中定义了onActivityResult,则不能跳过它并直接转到Fragment。但是,如果Activity不知道如何处理它,您可以将其重定向到Fragment。使用唯一的requestCodes来区分处理结果的人。
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);
}
}
注意:确保从片段中调用getActivity()。startActivityForResult(),而不是简单地使用this.startActivityForResult()
public void something(Intent intent) {
getActivity().startActivityForResult(intent);
// or if you are using SherlockActionBar/Support package
getSupportActivity().startActivityForResult(intent);
}
希望这有帮助。
答案 1 :(得分:1)
尝试删除片段super.onActivityResult(requestCode, resultCode, data);
中的行onActivityResult()
。
答案 2 :(得分:1)
public void onActivityResult
Activity
开始调用startActivityForResult
时,会调用{p> finish()
答案 3 :(得分:0)
首先调用Activity中的onActivityResult,不应该删除处理Activity中的结果,而是通过检查requestCode来执行您想要的操作。
答案 4 :(得分:0)
我也面临这个问题。
从片段
调用活动((活动)上下文).startActivityForResult(photoPickerIntent,100);
来自FragmentActivity的“上下文”。现在结果出现在FargmentActivity onActivityResult方法中。要切换此结果,您必须在fragmetn活动类中声明片段类的静态变量。static ActivityUploadPhotos activityUploadPhotos;
...
ActivityUploadPhotos f = new ActivityUploadPhotos();
ActivityHome.activityUploadPhotos=f;
......
if(activityUploadPhotos!=null)
{
activityUploadPhotos.onActivityResult(requestCode, resultCode, data);
}