android - 使用 TabHost 时出现错误

标签 android android-tabhost

我刚刚制作了一个在 Android 中使用 TabHost 的示例代码,但它导致了错误

Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

我不知道我错在哪里!

这是我的 LogCat,

01-10 11:13:42.700: D/AndroidRuntime(4062): Shutting down VM
01-10 11:13:42.710: W/dalvikvm(4062): threadid=1: thread exiting with uncaught exception (group=0x40a591f8)
01-10 11:13:42.730: E/AndroidRuntime(4062): FATAL EXCEPTION: main
01-10 11:13:42.730: E/AndroidRuntime(4062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.irctcdemo/com.example.irctcdemo.TabBar}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.os.Looper.loop(Looper.java:137)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at java.lang.reflect.Method.invoke(Method.java:511)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at dalvik.system.NativeStart.main(Native Method)
01-10 11:13:42.730: E/AndroidRuntime(4062): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.TabActivity.onContentChanged(TabActivity.java:131)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.Activity.setContentView(Activity.java:1835)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at com.example.irctcdemo.TabBar.onCreate(TabBar.java:20)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.Activity.performCreate(Activity.java:4465)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-10 11:13:42.730: E/AndroidRuntime(4062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

TabBar.java

TabHost tabHost;

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

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

      // Get TabHost Refference1
      tabHost = getTabHost();

      // Set TabChangeListener called when tab changed
      tabHost.setOnTabChangedListener(this);

      TabHost.TabSpec spec;
      Intent intent;

       /************* TAB1 ************/
      // Create  Intents to launch an Activity for the tab (to be reused)
      intent = new Intent().setClass(this, Tab1Activity.class);
      spec = tabHost.newTabSpec("First").setIndicator("")
                    .setContent(intent);

      //Add intent to tab
      tabHost.addTab(spec);

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

      // Set drawable images to tab
      //tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
      //tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);

      // Set Tab1 as Default tab and change image   
      tabHost.getTabWidget().setCurrentTab(0);
      //tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);


   }

firstscreen.xml代码,

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabhost">

<LinearLayout 
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <TabWidget 
        android:id="@+id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
    </TabWidget>

    <FrameLayout 
        android:id="@+id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    </FrameLayout>



   </LinearLayout>

</TabHost>

最佳答案

您的 Activity 应该扩展 TabActivity 而不是 Activity。

public class irctcdemo extends TabActivity {

然后使用

tabHost = getTabHost();

删除这个

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

您的 ID 应该是 android:id="@android:id/tabhost" for TabHostandroid:id="@android:id/tabs" 用于 TabWidget

关于android - 使用 TabHost 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21037170/

相关文章:

android - 有没有办法以编程方式获得显示刷新率?

android - BottomSheetDialog 禁用拖动

android - 为什么父Activity的菜单在TabHost中不显示?

android - Android TabActivity 中奇怪的选项菜单行为

android - 如何在android中动画绘制圆弧

android - ActionBarDrawerToggle 不能应用于 Android.support.v7.widget.Toolbar

android - ListView 的 setOnClickListener 不起作用

android - TabHost\WebView 嵌入式 youtube 视频 float 问题

Android FragmentTabHost

java - 使用动态创建的 fragment 将选项卡添加到嵌套的 FragmentTabHost