我正在尝试使用以下代码创建标签式活动
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;
}
}
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;
}
}
代码总是显示
FragmentManager中的。我搜索了seveal次,并通过almos所有的链接。但我无法得到任何解决方案。请帮帮我,因为我坚持这个。
----------------------------------------------- -------编辑---------------------------------
以上代码有时工作正常,有时会抛出异常。
答案 0 :(得分:1)
请尝试以下代码。
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);
答案 1 :(得分:0)
尝试使用:
if (savedInstanceState == null) {
// write code here
}
如果没有传达代码