[英]How is it possible that fragment's onActivityResult will be triggered during activity resuming
通常,這就是觸發片段的onActivityResult
。
public void onActivityResult(final int requestCode, final int resultCode, final Intent data)
Intent intent = new Intent(this.getActivity(), NewBuyPortfolioFragmentActivity.class);
startActivityForResult(intent, RequestCode.REQUEST_NEW_BUY_PORTFOLIO_FRAGMENT_ACTIVITY);
setResult(RESULT_OK, resultIntent);
finish();
但是,在極少數情況下(我無法復制),我將收到此類崩潰報告。
似乎在恢復活動期間,我的片段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個步驟的正常流程中,在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
通過執行以下操作,可能會在活動恢復期間觸發片段的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); finish();”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.