gpt4 book ai didi

Android Fragment backStack 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:52 26 4
gpt4 key购买 nike

我的 Activity 中有以下代码:

     public void categoryClicked(int categoryId, String categoryName) {                 


ItemList newFragment = ItemList.newInstance(categoryId);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.itemContainer, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}

它按预期工作,点击几次后我可以返回到之前的状态。但是,如果我只进行一次深入,则会出现以下异常:

03-10 22:17:19.895: ERROR/AndroidRuntime(23075): java.lang.IllegalStateException: Content view not yet created
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.ListFragment.ensureList(ListFragment.java:377)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.ListFragment.getListView(ListFragment.java:277)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at com.xxxxx.fragment.ItemList.onActivityCreated(ItemList.java:67)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:749)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:639)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1254)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:402)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.Activity.onBackPressed(Activity.java:2057)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.Activity.onKeyDown(Activity.java:1953)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.view.KeyEvent.dispatch(KeyEvent.java:2335)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.Activity.dispatchKeyEvent(Activity.java:2236)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2682)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2655)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.view.ViewRoot.handleMessage(ViewRoot.java:1952)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.os.Looper.loop(Looper.java:126)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at java.lang.reflect.Method.invoke(Method.java:491)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-10 22:17:19.895: ERROR/AndroidRuntime(23075): at dalvik.system.NativeStart.main(Native Method)

所以基本上,如果我只调用一次替换,当我点击后退按钮时它会出错。

此外,对于 ListFragment,我是否必须将背景设置为白色?我没有触摸它,我可以看到显示的旧 ListView ....

谢谢!

最佳答案

那么,您遇到的问题是没有可返回的 fragment 。这是你能做的。1:检查 fragment 是否存在于backstack中(通过给它一个名字)然后禁用返回2:当你第一次开始你的程序时,将一个 fragment 添加到后台堆栈,就像它被点击之前的初始状态一样。即使在这种情况下,您仍然需要进行检查以查看后台堆栈中是否还有任何内容可以返回。

关于Android Fragment backStack 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5268680/

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