gpt4 book ai didi

android - 在 Activity 恢复期间如何触发 fragment 的 onActivityResult

转载 作者:行者123 更新时间:2023-11-30 03:26:30 25 4
gpt4 key购买 nike

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

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

(1) 从fragment通过startActivityForResult启动一个intent

Intent intent = new Intent(this.getActivity(), NewBuyPortfolioFragmentActivity.class);
startActivityForResult(intent, RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY);

(2) 设置结果并从启动的 Activity 结束

setResult(RESULT_OK, resultIntent);
finish();

(3) Fragment的onActivityResult会被触发

但是,在极少数情况下(我无法重现),我会收到这样的崩溃报告。

似乎在 Activity 恢复期间,我的 fragment onActivityResult 将被触发,其 requestCode 与我的应用程序 requestCode 匹配(RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY ).

知道为什么在 Activity 恢复期间会触发 fragment 的 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 个步骤的正常流程中在 onActivityResult 中抛出 NPE,我将得到下面的堆栈跟踪,而没有 Unable to resume activity消息。

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

FATAL EXCEPTION: main
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:3141)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.yccheok.jstock.gui.portfolio.BuyPortfolioFragment.addTransaction(BuyPortfolioFragment.java:734)
at org.yccheok.jstock.gui.portfolio.PortfolioFragment.onActivityResult(PortfolioFragment.java:661)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
at org.yccheok.jstock.gui.JStockFragmentActivity.onActivityResult(JStockFragmentActivity.java:988)
at android.app.Activity.dispatchActivityResult(Activity.java:5192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
... 11 more

最佳答案

这样做可能会在 Activity 恢复期间触发 fragment 的 onActivityResult-

实际上我不明白你是如何管理你的 fragment 的,但我在上面的代码的基础上为你提供了两个解决方案。

1. 在您的情况下,它是空指针异常,因此您可以在 fragment Activity 的 super.onActivityResult 之前检查空值。

2.只需输入您的代码 "Intent intent = new Intent(this.getActivity(), NewBuyPortfolioFragmentActivity.class);startActivityForResult(intent, RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY);" 单击 fragment 项目或您想要打开 NewBuyPortfolioFragmentActivity 的任何位置,然后从那里开始 "Intent resultIntent; resultIntent = new Intent(this, ImageFragment.class); setResult(RESULT_OK, resultIntent); 完成();”

关于android - 在 Activity 恢复期间如何触发 fragment 的 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18141076/

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