如何在活动恢复期间触发片段的onActivityResult

时间:2013-08-09 06:20:17

标签: android

通常,这就是我的片段onActivityResult被触发的方式。

public void onActivityResult(final int requestCode, final int resultCode, final Intent data)

(1)通过片段

中的startActivityForResult启动意图
Intent intent = new Intent(this.getActivity(), NewBuyPortfolioFragmentActivity.class);
startActivityForResult(intent, RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY);

(2)从已启动的活动

设置结果并完成
setResult(RESULT_OK, resultIntent);
finish();

(3)片段的onActivityResult将被触发

但是,在非常罕见的情况下(我无法重现),我会收到此类崩溃报告。

似乎在活动恢复期间,我的片段onActivityResult将被触发,其requestCode与我的应用requestCode匹配(RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY)。

知道为什么片段的onActivityResult会在活动恢复期间被触发?

java.lang.RuntimeException: Unable to resume activity {org.yccheok.jstock.gui/org.yccheok.jstock.gui.JStockFragmentActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196615, result=-1, data=Intent { (has extras) }} to activity {org.yccheok.jstock.gui/org.yccheok.jstock.gui.JStockFragmentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196615, result=-1, data=Intent { (has extras) }} to activity {org.yccheok.jstock.gui/org.yccheok.jstock.gui.JStockFragmentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
... 13 more
Caused by: java.lang.NullPointerException
at org.yccheok.jstock.gui.portfolio.BuyPortfolioFragment.void addTransaction(org.yccheok.jstock.portfolio.Transaction)(SourceFile:733)
at org.yccheok.jstock.gui.portfolio.PortfolioFragment.void onActivityResult(int,int,android.content.Intent)(SourceFile:661)
at android.support.v4.app.FragmentActivity.void onActivityResult(int,int,android.content.Intent)(SourceFile:161)
at org.yccheok.jstock.gui.JStockFragmentActivity.void onActivityResult(int,int,android.content.Intent)(SourceFile:988)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
... 14 more

注意,如果在我上面提到的3个步骤正常流程中{1}}中抛出NPE,我将得到以下堆栈跟踪,没有onActivityResult消息。

正常onActivityResult流产生的崩溃报告。

Unable to resume activity

1 个答案:

答案 0 :(得分:1)

片段的onActivityResult可能会在活动恢复期间被触发 -

实际上我没有得到你如何管理你的片段,但我提供了两个解决方案......基于上面的代码。

1。在您的情况下,它是空指针异常,因此您可以在片段活动的super.onActivityResult之前检查null。

<强> 2 只需输入你的代码“Intent intent = new Intent(this.getActivity(),NewBuyPortfolioFragmentActivity.class); startActivityForResult(intent,RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY);“点击片段项或任何你想要打开NewBuyPortfolioFragmentActivity的地方,只需”Intent resultIntent;         resultIntent = new Intent(this,ImageFragment.class);         setResult(RESULT_OK,resultIntent);         光洁度();“