user3312569
user3312569

Reputation: 9

Android- Tab Activity giving ERROR

I have to create two tabs.And the "TabHost tabHost = getTabHost(); "is giving me error saying the method getTabHost() is undefine in the MainActivity.

My mainActivity.java file : package com.example.bckup;

import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.content.Context;

public class MainActivity extends Activity {

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

        Resources ressources = getResources(); 

       TabHost tabHost = getTabHost();        

        // Android tab
        Intent intentAndroid = new Intent().setClass(this, line1.class);
        TabSpec tabSpecAndroid = tabHost.newTabSpec("Android")
                                .setContent(intentAndroid);

        //Linux tab
        Intent intentLinux = new Intent().setClass(this, line2.class);
        TabSpec tabSpecLinux = tabHost.newTabSpec("Linux")
                   .setContent(intentLinux);    



        // add all tabs 
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecLinux);


        //set Windows tab as default (zero based)
        tabHost.setCurrentTab(0);      
    }

}

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

*/

line1.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class line1 extends Activity {

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

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.friends_list, menu);
        return true;
    }*/
} 

acivity_main.xml

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

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@+id/tabFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>

    </TabHost>

Can any one help me to fix it please.

Upvotes: 0

Views: 616

Answers (2)

mangusta
mangusta

Reputation: 3542

use

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

instead of

TabHost tabHost = getTabHost();

and extend TabActivity of course

Upvotes: 0

Zied R.
Zied R.

Reputation: 4964

Your main Activity should extend TabActivity

public class MainActivity extends TabActivity 

Upvotes: 2

Related Questions