gpt4 book ai didi

android - 从 AppCompatActivity 返回后显示 DialogFragment 时出现 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 01:20:36 26 4
gpt4 key购买 nike

我从自定义 DialogFragment 启动 FragmentActivity,在我返回原始 Activity 并尝试再次打开对话框后,我得到:

IllegalStateException: Can not perform this action after onSaveInstanceState

我不明白为什么会这样,如果我从原始 Activity 中关闭对话框,那么我可以根据需要多次显示它,但是如果我从 DialogFragment 开始一个新的 Activity 之后我就无法显示再次出现任何对话,因为我得到了异常。

这是我的代码,这个方法在我的 DialogFragment 中:

 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){
mSearchLocationType = position;
switch(position){
case PSL_CURRENT_POSITION: // I can show the dialog again after this.
break;
case PSL_MAP_POINT:
Intent selectMapPoint = new Intent(parentActivity, SelectMapPointActivity.class);
selectMapPoint.putExtra(SelectMapPointFragment.EXTRA_SELECTED_POS, mSearchLocation);
parentActivity.startActivityForResult(selectMapPoint, REQ_MAP_POINT);
// After returning this Activity and trying to show ANY DialogFragment the app crashes.
break;
case PSL_ADDRESS:
Intent selectAddress = new Intent(parentActivity, SelectAddressActivity.class);
parentActivity.startActivity(selectAddress); // Also from this Activity, makes no difference.
break;
}
dismiss();
}

我正在显示这样的对话框:

if(!selectPoiAroundDialog.isVisible())
selectPoiAroundDialog.show(parentActivity.getSupportFragmentManager(), "mSelectPoiCategoryDialog");

我已经尝试过的:

  • 覆盖 DialogFragment 的 show() 方法并使用此代码显示对话框:

    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commitAllowingStateLoss();

  • 调试应用程序并检查是否调用了所有生命周期方法,但对我来说似乎很正常,每次都会调用 DialogFragment 的 onDestroyView() 方法。

  • 覆盖 DialogFragment 的 show() 和 onDismiss() 方法并跟踪它是否显示。

这是一个公司应用程序,设计很糟糕,所以我试图修改的类(我正在显示对话框的地方)不是 Activity 或 Fragment 的子类。 “parentActivity”由应用程序类的静态方法访问,但我确信它具有正确的 Activity。您认为这会导致问题吗?

编辑:完整的堆栈跟踪是在我使用 commitAllowStateLoss() 时:

 java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
at com.mycompany.dialog.GTDialog.show(GTDialog.java:174)
at com.mycompany.fragment.content.SearchOfflinePOIContent$2.onClick(SearchOfflinePOIContent.java:250)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
  • 我还尝试覆盖 onSaveInstanceState() 方法,但没有帮助。

编辑 2:原始堆栈跟踪是:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
at com.mycompany.dialog.GTDialog.show(GTDialog.java:174)
at com.mycompany.fragment.content.SearchOfflinePOIContent$1.onClick(SearchOfflinePOIContent.java:197)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

如果我覆盖 show() 方法并尝试使用 commitAllowingStateLoss() 提交 fragment ,那么我会得到第一个异常。

最佳答案

我发现了问题。当应用程序从 Activity 退出时,它并没有从 parentActivity 中删除对它的引用,所以当我再次尝试显示对话框时,我给出了被销毁的 Activity 的引用。

感谢您的宝贵时间!

关于android - 从 AppCompatActivity 返回后显示 DialogFragment 时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37010047/

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