在我的应用程序中,我有一个 SherlockFragmentActivity,它托管一个带有 4 个选项卡的操作栏。每个选项卡都是一个 SherlockFragment。从其中一个选项卡(比如说 Tab1)调用普通 Activity 与 startActivityForResult(myIntent1, 0) 一起工作正常;
但是我在 Tab1 中的 onActivityResult 永远不会被调用。在阅读了很多主题后,我了解到我的主要活动的 onActivityResult 是首先被调用的,这确实是。但是我无法将值传递给我的片段的 onActivityResult。
这是我的代码...
主要活动:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment mFragment = Fragment.instantiate(this, Tab1.class.getName());
mFragment.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
还尝试在第一行使用超级呼叫而没有超级呼叫。
分段:
public void onActivityResult(int requestCode, int resultCode, Intent data){
//super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(context, String.valueOf(requestCode), Toast.LENGTH_SHORT).show();
if(requestCode == 0) {
if(resultCode == Activity.RESULT_OK) {
//do some stuff here
}
}
}
还尝试在开始和结束时使用超级呼叫。
每次我得到同样的错误:
09-09 14:33:34.230: E/AndroidRuntime(7408): FATAL EXCEPTION: main
09-09 14:33:34.230: E/AndroidRuntime(7408): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=null} to activity {com.BlubBlub.TimeMate/com.BlubBlub.TimeMate.TimeMateActivity}: java.lang.NullPointerException
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread.deliverResults(ActivityThread.java:3267)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3310)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread.access$1100(ActivityThread.java:142)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.os.Looper.loop(Looper.java:137)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread.main(ActivityThread.java:4930)
09-09 14:33:34.230: E/AndroidRuntime(7408): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 14:33:34.230: E/AndroidRuntime(7408): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 14:33:34.230: E/AndroidRuntime(7408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-09 14:33:34.230: E/AndroidRuntime(7408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-09 14:33:34.230: E/AndroidRuntime(7408): at dalvik.system.NativeStart.main(Native Method)
09-09 14:33:34.230: E/AndroidRuntime(7408): Caused by: java.lang.NullPointerException
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.widget.Toast.<init>(Toast.java:92)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.widget.Toast.makeText(Toast.java:238)
09-09 14:33:34.230: E/AndroidRuntime(7408): at com.BlubBlub.TimeMate.Tab1.onActivityResult(Tab1.java:125)
09-09 14:33:34.230: E/AndroidRuntime(7408): at com.BlubBlub.TimeMate.TimeMateActivity.onActivityResult(TimeMateActivity.java:150)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
09-09 14:33:34.230: E/AndroidRuntime(7408): at android.app.ActivityThread.deliverResults(ActivityThread.java:3263)
什么是让它工作的正确方法?我现在真的很绝望……