Tabhost抛出运行时错误 - 代码对我来说似乎很好 - 但显然不是。我想在一些标签中显示2个活动。
错误
(java.lang.IllegalArgumentException:您必须指定一种创建标签内容的方法)
MainActivity
public class MainActivity extends TabActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab 1
TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
// setting Title and Icon for the Tab
tab1.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab1Intent = new Intent(this, Tab1Activity.class);
tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab1.setContent(tab1Intent);
// Tab 2
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
// setting Title and Icon for the Tab
tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab2Intent = new Intent(this, Tab2Activity.class);
tab1Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab1.setContent(tab2Intent);
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dawnlp.innertab.MainActivity"
android:background="#00547d"
android:textColor="#FFFFFF"
>
<TabHost
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"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
不要认为这与我的其他活动有关。
答案 0 :(得分:1)
将上次标签代码更改为: 您使用tab1而不是tab2作为指标
// Tab 2
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
// setting Title and Icon for the Tab
tab2.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
Intent tab2Intent = new Intent(this, Tab2Activity.class);
tab2Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tab2.setContent(tab2Intent);