首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android TabHost示例

Android TabHost示例
EN

Stack Overflow用户
提问于 2014-03-17 20:24:15
回答 4查看 35.5K关注 0票数 2

我做了,做了个例子。但我会犯错。

代码语言:javascript
复制
Error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabmenu/com.example.tabmenu.MainActivity: java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.

mainActivity.java

代码语言:javascript
复制
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabSpec tab3 = tabHost.newTabSpec("Third Tab");

        tab1.setIndicator("Tab1");
        tab1.setContent(new Intent(this,Tab1Activity.class));

        tab1.setIndicator("Tab2");
        tab1.setContent(new Intent(this,Tab2Activity.class));

        tab1.setIndicator("Tab3");
        tab1.setContent(new Intent(this,Tab3Activity.class));

        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
    }
}

androidManifest.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tabmenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity 
            android:name="com.example.tabmenu.Tab1Activity"/>
        <activity
            android:name="com.example.tabmenu.Tab2Activity"/>
        <activity
            android:name="com.example.tabmenu.Tab3Activity"/>
        <activity
            android:name="com.example.tabmenu.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-17 20:43:13

尝试更改此部分:

代码语言:javascript
复制
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));

tab1.setIndicator("Tab2");
tab1.setContent(new Intent(this,Tab2Activity.class));

tab1.setIndicator("Tab3");
tab1.setContent(new Intent(this,Tab3Activity.class));

下面的那个..。就像这样:

代码语言:javascript
复制
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));

--> tab2.setIndicator("Tab2");
--> tab2.setContent(new Intent(this,Tab2Activity.class));

--> tab3.setIndicator("Tab3");
--> tab3.setContent(new Intent(this,Tab3Activity.class));

错误是因为您没有使用tab2或tab3,也没有重复使用tab1。

希望这能有所帮助。

票数 11
EN

Stack Overflow用户

发布于 2014-03-17 20:31:03

像这样修改你的清单(布努·德内米辛宣言)

代码语言:javascript
复制
<activity
    android:name="com.example.tabmenu.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>

     <activity
        android:name="com.example.tabmenu.Tab1Activity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.example.tabmenu.Tab2Activity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
        android:name="com.example.tabmenu.Tab2Activity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.example.restaurant.Tab2Activity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.tabmenu.Tab3Activity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.example.tabmenu.Tab3Activity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
票数 0
EN

Stack Overflow用户

发布于 2014-10-27 06:28:27

试试这个code...it可能会有帮助

代码语言:javascript
复制
public class Test extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, Tab1Activity.class);
    spec = tabHost.newTabSpec("First").setIndicator("First")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab2Activity.class);
    spec = tabHost.newTabSpec("Second").setIndicator("Second")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab3Activity.class);
    spec = tabHost.newTabSpec("Third").setIndicator("Third")
                  .setContent(intent);
    tabHost.addTab(spec);


}

相应的xml页面.

代码语言:javascript
复制
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
          android:id="@android:id/tabhost">

        <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22464583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档