【问题标题】:Android TabHost ExampleAndroid TabHost 示例
【发布时间】:2014-03-17 20:24:15
【问题描述】:

我做了并运行了示例。但我犯了错误。

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

@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

<?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>

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    尝试更改此段:

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

    与下面的......像这样:

    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。

    希望这会有所帮助。

    【讨论】:

    • @c.mert 很高兴为您提供帮助!
    【解决方案2】:

    像这样修改你的清单(Bunu denermisin manifestinde)

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

    【讨论】:

    • Sorunun 之类的 kısmını hallettim。她的 sınıf için ayrı 活动 etiketi açmam gerekiyormuş。 Şuan başka bir hata alıyorum。错误:您必须指定创建选项卡指示器的方法。
    • @c.mert 请用英文写:)
    • 好吧,我会写英文。您在主页中的哪里出错了。
    • @Ziprox09 他解决了清单部分的问题,但他现在正在处理新问题,即错误:您必须指定创建选项卡指示器的方法。
    • @Ziprox09 抱歉 :) 现在我发现了我的问题 :) 我应该写; 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));但是我为每个人都使用了 tab1,所以它没有用 :) 但现在它正在工作。谢谢大家。
    【解决方案3】:

    试试这个代码...它可能会有所帮助

    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页面......

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

    【讨论】:

      【解决方案4】:

      更改给定的语句

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

      TabHost tabHost = getTabHost();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 2011-09-27
        相关资源
        最近更新 更多