Android Tab问题抛出运行时(虽然对我来说看起来不错)



tabhost抛出运行时错误 - 代码对我来说似乎还不错 - 但显然不是。我有2个活动要在某些标签中显示。

错误

(java.lang.illegalgumentException:您必须指定一种创建选项卡内容的方法)

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);

相关内容

最新更新