【发布时间】:2014-05-05 10:14:35
【问题描述】:
我有一个TabHost,我想动态创建标签。我看过很多例子,但它们不起作用。
TabHost tabHost;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setIndicator("First Tab");
tab1.setContent(new Intent(this,MainActivity2.class));
tabHost.addTab(tab1);
这是我尝试过的最后一件事,但它不起作用。这是我考虑过的一些链接。我找不到我做错了什么。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.LH.tabletres.MainActivity">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
【问题讨论】:
-
为什么不工作?给我简要信息
-
@Blast 我建议你现在应该切换到 ActionBar 选项卡。
-
我不知道。当我尝试运行它时,android stuido 中有很多错误。其中一些在这里
Caused by: java.lang.NullPointerExceptionat com.LH.tabletres.MainActivity.onCreate(MainActivity.java:18)` at android.app.Activity.performCreate(Activity.java:5008)` Line:18 包括tabHost.setup(); -
@Blast 显示你的布局,
activity_main.xml -
@nikis 我已经添加了 activty_main.xml
标签: android android-layout android-studio