Android 选项卡问题导致运行时异常(不过我觉得还不错)
Android Tab Issue throwing up runtime (looks fine to me though)
Tabhost 抛出运行时错误 - 代码对我来说似乎没问题 - 但显然不是。我有 2 个活动要在某些选项卡中显示。
错误
(java.lang.IllegalArgumentException: you must specify a way to create the tab content)
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>
不要认为这与我的其他活动有任何关系。
将最后一个标签代码更改为:
您正在使用 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);
Tabhost 抛出运行时错误 - 代码对我来说似乎没问题 - 但显然不是。我有 2 个活动要在某些选项卡中显示。
错误
(java.lang.IllegalArgumentException: you must specify a way to create the tab content)
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>
不要认为这与我的其他活动有任何关系。
将最后一个标签代码更改为: 您正在使用 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);