本文介绍了得到错误使用TabHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚作出了一个样本code在Android中使用TabHost但它造成误差

I dont know where i am wrong!

Here's my 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 code,

<?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>
解决方案

Your activity should extend TabActivity not Activity.

public class irctcdemo extends TabActivity {

and just use

tabHost = getTabHost();

remove this

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

and your Id should be android:id="@android:id/tabhost" for TabHost and android:id="@android:id/tabs" for TabWidget

这篇关于得到错误使用TabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:03