gpt4 book ai didi

android - 回到 FragmentActivity 后出现 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 14:02:57 27 4
gpt4 key购买 nike

这是关于我发现我的应用程序面临的一个反复出现的问题,它与使用几个 DialogFragment 相关。我主要针对平台级别 8 设备,因此要使用 DialogFragments,我必须使用兼容性库。

每当我退出应用程序并且他们回来时,问题本身就会发生。这是堆栈跟踪:

12-22 14:56:05.819: W/dalvikvm(6384): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
12-22 14:56:05.949: E/AndroidRuntime(6384): FATAL EXCEPTION: main
12-22 14:56:05.949: E/AndroidRuntime(6384): java.lang.IllegalStateException: Activity has been destroyed
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1269)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:529)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:102)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.view.ViewPager.populate(ViewPager.java:692)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:851)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:123)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 14:56:05.949: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 14:56:05.949: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 14:56:05.949: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-22 14:56:05.949: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-22 14:56:05.949: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)

这是我精简到最低限度的 MainActivity。

public class MainActivity extends FragmentActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
...
public void showNewChannelFragmentDialog() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
NewChannelDialogFragment newFragment = NewChannelDialogFragment.newInstance();
newFragment.setContext(this);
newFragment.show(ft,"dialog");
}
}

showNewChannelFragmentDialog() 方法仅在用户选择特定菜单选项时调用,因此我认为 DialogFragment 本身的内容在这里无关紧要。

但你可以看到我完全按照显示的方式做了 here它确实有效,正如我之前所说的问题是每当用户离开后我的应用程序就会恢复正常。

有什么建议吗?我正在考虑删除 fragment 并仅使用常规对话框。

最佳答案

也许这不是唯一的问题,但你应该打电话

ft.add(newFragment);
ft.commit(); //but at least this

showNewChannelFragmentDialog() 函数的末尾。

关于android - 回到 FragmentActivity 后出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607491/

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