简体   繁体   中英

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState while using FragmentTabHost

I am trying create a tabbed activity using the below code

 public class FragmentTabs extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), SimpleFrag.class, null); mTabHost.addTab(mTabHost.newTabSpec("detailed").setIndicator("Detailed"), DetailedFrag.class, null); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } } 

The code always shows java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState inside FragmentManager. I googled seveal times and goes through almos all the links. but i can't get any solution. Please help me because i'm stuck on this.

------------------------------------------------------Edit---------------------------------

The above code working fine sometimes and throws exception sometimes.

please try the following code.

super.onCreate(savedInstanceState);
View root = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate(R.layout.activity_main, null, false);
setContentView(root);
mTabHost = (FragmentTabHost) root.findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),SimpleFrag.class, null);
mTabHost.addTab(mTabHost.newTabSpec("detailed").setIndicator("Detailed"), DetailedFrag.class, null);

try using this:

if (savedInstanceState == null) {
        // write code here
    }

if not convey the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM