Error : Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs

StackOverflow https://stackoverflow.com/questions/21301388

  •  01-10-2022
  •  | 
  •  

Вопрос

I'm new to Android and developing. I'm using Tab host widget. What I'm doing is I made 3 Java classes and corresponding xml files. Each Java class can load its xml. When someone presses the tab then it redirects to above Java classes. Please find the below code that I used.Now im getting Errors please find below code I used.

CompareActivity.java

package com.example.comparison.lk;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.NotificationCompat.Action;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.tabs.*;

public class CompareActivity extends TabActivity {

    TabHost tabHost;
    Intent intent;
    TabHost.TabSpec spec;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_compare);

        ActionBar actionbr= getActionBar();
        actionbr.setBackgroundDrawable(new ColorDrawable(0XFF1c3c4d));
        intialiseTabWidgets();
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
               @Override
               public void onTabChanged(String tabId) {

               }
            });



    }

    public void intialiseTabWidgets()
    {
        //TabHost main_tab=(TabHost)findViewById(R.id.tabhost);
        //main_tab.setup();

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

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


        /************* TAB2 ************/
        // Create Intents to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, compareNow.class);
        spec = tabHost.newTabSpec("second").setIndicator("").setContent(intent);
        // Add intent to tab
        tabHost.addTab(spec);

        /************* TAB3 ************/
        intent = new Intent().setClass(this, Search.class);
        spec = tabHost.newTabSpec("third").setIndicator("").setContent(intent);
        tabHost.addTab(spec);

    }

comapreNow.java/Login.java/Search.java all these classes using same stuture

package com.tabs;

import com.example.comparison.lk.R;


import android.app.Activity;
import android.app.TabActivity;


import android.os.Bundle;
import android.widget.TabHost.OnTabChangeListener;


public class compareNow extends TabActivity implements OnTabChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_compare);






    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub

    }


} 

activity_compare.xml

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/topbar" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >
        </RelativeLayout>

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


        </TabHost>

    </LinearLayout>

</FrameLayout>

compare_now.xml/login.xml/search.xml all these files have same stuture

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

AndroidManifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.comparison.lk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"

        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme"

         >
        <activity
            android:name="com.example.comparison.lk.CompareActivity"
            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.tabs.compareNow"></activity>
         <activity android:name="com.tabs.Login"></activity>
         <activity android:name="com.tabs.Search"></activity>



    </application>

</manifest>

Logcat Error

 01-23 12:13:08.968: E/AndroidRuntime(27433): FATAL EXCEPTION: main
01-23 12:13:08.968: E/AndroidRuntime(27433): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.comparison.lk/com.example.comparison.lk.CompareActivity}: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.os.Looper.loop(Looper.java:137)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread.main(ActivityThread.java:4511)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at dalvik.system.NativeStart.main(Native Method)
01-23 12:13:08.968: E/AndroidRuntime(27433): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.widget.TabHost.setup(TabHost.java:124)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.widget.TabHost.setup(TabHost.java:178)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.TabActivity.onContentChanged(TabActivity.java:135)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:275)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.Activity.setContentView(Activity.java:1835)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at com.example.comparison.lk.CompareActivity.onCreate(CompareActivity.java:25)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.Activity.performCreate(Activity.java:4470)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-23 12:13:08.968: E/AndroidRuntime(27433):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-23 12:13:08.968: E/AndroidRuntime(27433):    ... 11 more
Это было полезно?

Решение

You're having error with xml and public void intialiseTabWidgets() method

xml should be changed as:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

      <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

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

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                     android:background="#160203" />

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

            </LinearLayout>

        </TabHost>

</FrameLayout>

and use intialiseTabWidgets() method should have null references oncreate method

TabHost tabHost = getTabHost(); 

should be changed to

tabHost = getTabHost();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top