我试图在FragmentActivity中的v4支持库的Fragment中使用FragmentTabHost。调试时会在Activity OnCreate中抛出此异常,因为Alex已指出可能的情况,但仍在活动oncreate中提交片段事务。由于此异常,我的活动显示为带有选项卡的空tabcontent。
注意: - 1)我锁定了屏幕旋转 - 没有方向条件。 2)这发生在初始状态中的活动oncreate中 3)我在Activity OnCreate()中使用了commit() 4)作为日志点,提交关系在TabHost中完成而不是从我这边完成。
这是我的日志: -
03-29 11:32:55.975: E/AndroidRuntime(27470): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:282)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.View.dispatchAttachedToWindow(View.java:9924)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2198)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1079)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.os.Looper.loop(Looper.java:137)
03-29 11:32:55.975: E/AndroidRuntime(27470): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-29 11:32:55.975: E/AndroidRuntime(27470): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 11:32:55.975: E/AndroidRuntime(27470): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 11:32:55.975: E/AndroidRuntime(27470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-29 11:32:55.975: E/AndroidRuntime(27470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-29 11:32:55.975: E/AndroidRuntime(27470): at dalvik.system.NativeStart.main(Native Method)
这是我的代码
的活动:
private void addFragmentToStack(int originCurrentSplitNo, boolean isOrgin,
int holderId) {
// Instantiate a new fragment(FragmentTabhost as the layout of this).
SplitFragment newFragment = SplitFragment.newInstance();
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(holderId, (Fragment) newFragment,
newFragment.getCurrentFragmentTag());
if (null == fragmentTagHolder) {
fragmentTagHolder = new ArrayList<String>();
}
//tag holders in the activity
fragmentTagHolder.add(newFragment.getCurrentFragmentTag());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
具有将附加到活动的FragmentTabHost的片段
// SplitFragment.java
private View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// Currently in a layout without a container, so no
// reason to create our view.
return null;
}
View fragmentTabHostView = inflater.inflate(R.layout.fragment_tab_host_layout, container,
false);
initFrgmentTab(fragmentTabHostView );
return fragmentTabHostView;
}
private View initFrgmentTab(View oemView) {
if (null == fragmentTabHostView ) {
return null;
}
typeViewHost = (FragmentTabHost) fragmentTabHostView
.findViewById(android.R.id.tabhost);
if (!(null == typeViewHost)) {
typeViewHost.setup(getActivity().getApplicationContext(), getChildFragmentManager(),android.R.id.tabcontent);
LinkedHashMap<Integer, fragmentTabContent> frgmentTabContent = ((OEMCarInspectionActivity) getActivity())
.getfrgmentTabContent Details();
Set<Integer> views = frgmentTabContent .getKeySet();
//Creating the tabs
for (Integer viewCode : views) {
fragmentTabContent carView = carTypeViews.get(viewCode );
String tabTitle = fragmentTabContent.getViewDescription();
if (null == tabTitle || tabTitle .trim().isEmpty()) {
continue;
}
Bundle tabViewBundle = new Bundle();
carViewBundle.putInt(CarViewFragment.CARVIEW_CODE_KEY,
viewCode);
typeViewHost.addTab(typeViewHost.newTabSpec(tabTitle )
.setIndicator(tabTitle ), TabViewFragment.class,
tabViewBundle);
}
}
片段作为FragmentTabHost的tabcontent:
//TabViewFragment.java
/**
* The Fragment's UI is just a simple text view showing its instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (null == container) {
// Currently in a layout without a container, so no
// reason to create our view.
return null;
}
View v = inflater.inflate(R.layout.tab_view_layout, container,
false);
return v;
}
FragmentTabHost的布局:
//fragment_tab_host_layout.xml(layout for SplitFragment.java )
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
答案 0 :(得分:0)
最初我认为这很可能是轮换问题。但是,根据您的其他详细信息,我认为我发现了问题。
您正在片段onCreateView中初始化tabhost。在onCreateView中,片段尚未添加到活动中;只是要求提供它的布局。会话状态可能尚未恢复(就片段事务管理器而言),因为它基本上处于构建状态(正在插入视图)的中间。 / p>
尝试将tabhost初始化代码移动到onActivityCreated,这是在插入视图后调用的。 I.E:
private View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// Currently in a layout without a container, so no
// reason to create our view.
return null;
}
View fragmentTabHostView = inflater.inflate(R.layout.fragment_tab_host_layout, container,
false);
return fragmentTabHostView;
}
public void onActivityCreated (Bundle savedInstanceState) {
initFrgmentTab(getView());
}
如果仍然会出现混乱的情况,请尝试将其移至onResume(肯定是在状态恢复后)。
答案 1 :(得分:0)
我有一个解决方案:
它很难看,但它有效。