android – SupportFragment TabWidget三星设备=“java.lang.IllegalStateException:onSaveInstanceState后无法执行此
作者:互联网
我有一个非常简单的FragmentActivity,使用android.support.v4库,
FragmentActivity中只有一个TabHost,它包含4个片段.
没有异步任务,没有onSaveInstanceState()的实现.
在市场上几周之后我得到了很多异常报告,名为“java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作”(请参阅下面的堆栈跟踪),全部来自三星设备(SM-N9003,GT-I9305,GT) -N7100 ……等等,完全不知道出了什么问题.
我之前搜索过一些类似的问题,但根本没有答案.
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
代码:
private void setupTabs() {
FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(setIndicator(this, tabHost.newTabSpec(FragmentList), getString(R.string.tabwidget_List), R.drawable.btn_icon_order), ListFragment.class, null);
tabHost.addTab(setIndicator(this, tabHost.newTabSpec(FragmentSwitch), getString(R.string.tabwidget_Switch), R.drawable.btn_icon_switch), SwitchFragment.class, null);
tabHost.addTab(setIndicator(this, tabHost.newTabSpec(FragmentQueue), getString(R.string.tabwidget_Queue), R.drawable.btn_icon_queue), QueueFragment.class, null);
tabHost.addTab(setIndicator(this, tabHost.newTabSpec(FragmentMore), getString(R.string.tabwidget_More), R.drawable.btn_icon_more), MoreFragment.class, null);
}
private TabSpec setIndicator(Context context, TabSpec tabSpec, String Title,int resId) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_widget, null);
ImageView ivTTIcon = (ImageView) view.findViewById(R.id.ivTTIcon);
ivTTIcon.setImageResource(resId);
TextView tvTTText = (TextView) view.findViewById(R.id.tvTTText);
tvTTText.setText(Title);
return tabSpec.setIndicator(view);
}
堆栈跟踪 :
java.lang.IllegalStateException: Can not perform this action after
onSaveInstanceState at
android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
at
android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1354)
at
android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at
android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at
android.support.v4.app.FragmentTabHost.onTabChanged(FragmentTabHost.java:309)
at
android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:463)
at android.widget.TabHost.setCurrentTab(TabHost.java:448) at
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
at
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:563)
at android.view.View.performClick(View.java:4475) at
android.view.View$PerformClick.run(View.java:18784) at
android.os.Handler.handleCallback(Handler.java:730) at
android.os.Handler.dispatchMessage(Handler.java:92) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:5414) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:525) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
解决方法:
这是解决方案,
如果在调用onSaveInstanceState之后执行选项卡选择操作,则会出现此问题.例如,如果用户选择并保持任何选项卡,同时也选择主页按钮.
要解决这个问题,只需调用mTabHost.getTabWidget().setEnabled(false);在onPause of Fragment / Activity下并调用mTabHost.getTabWidget().setEnabled(true);在onResume下.
标签:android,android-fragments,tabwidget 来源: https://codeday.me/bug/20190825/1718172.html