简体   繁体   中英

Android Tab widget

I am using below code to create three tab in activity but it gives me null pointer exception when I add tab using addtab method.

            tabHost.addTab(firstTabSpec);//Null Pointer error occured here
            tabHost.addTab(secondTabSpec);
            tabHost.addTab(thirdTabSpec);

Below is my Code both layout xml file and onCreaate() method where I create tabs and put actvity in it.

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            try {
                /** TabHost will have Tabs */
                TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

                /**
                 * TabSpec used to create a new tab. By using TabSpec only we can
                 * able to setContent to the tab. By using TabSpec setIndicator() we
                 * can set name to tab.
                 */

                /** tid1 is firstTabSpec Id. Its used to access outside. */
                TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
                TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
                TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");

                /** TabSpec setIndicator() is used to set name for the tab. */
                /**
                 * TabSpec setContent() is used to set content for a particular tab.
                 */
                firstTabSpec.setIndicator("Update-1").setContent(
                        new Intent(this, Keyboard.class));
                secondTabSpec.setIndicator("Update-2").setContent(
                        new Intent(this, Sensors.class));
                thirdTabSpec.setIndicator("Update-3").setContent(
                        new Intent(this, Relays.class));

                /** Add tabSpec to the TabHost to display. */
                tabHost.addTab(firstTabSpec);//Null Pointer error occured here
                tabHost.addTab(secondTabSpec);
                tabHost.addTab(thirdTabSpec);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

Layout main.xml file code


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

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

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

</TabHost>

Edited XML file

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



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

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

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

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

</TabHost>

</LinearLayout>

Logcat:

 java.lang.NullPointerException
    at android.widget.TabHost.addTab(TabHost.java:221)
    at com.android.embededconversation.EmbededConversation.onCreate(EmbededConversation.java:51)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)

This is wrong

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

you are trying to inflate from the wrong resource and the tabHost stays null, always.

change it to :

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

And make sure you include the correct R (your project's R, not android.R)

EDIT

If you're using TabActivity, the way to fetch the TabHost is, I added an example from an application I wrote:

your TabActivity :

public class yourTabActivity extends TabActivity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab_widget);

        // fetch the TabHost
        final TabHost tabHost = (TabHost) getTabHost();

        // create Tabs
        tabHost.addTab(createTab(yourActivity.class, 
                "tag", "yourTabTitle", R.drawable.tv));
    }

    private TabSpec createTab(final Class<?> intentClass, final String tag, final String title, final int drawable)
    {
        final Intent intent = new Intent().setClass(this, intentClass);
        final View tab = LayoutInflater.from(getTabHost().getContext()).inflate(R.layout.tab, null);
        ((TextView) tab.findViewById(R.id.tab_text)).setText(title);
        ((ImageView) tab.findViewById(R.id.tab_icon)).setImageResource(drawable);

        return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
    }

}

main_tab_widget.xml

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

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

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

            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none" >

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

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

</LinearLayout>

example for tab.xml. this is actually a custom tab.

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:background="@drawable/tab_bg_selector" 
        android:gravity="center"
        android:orientation="vertical" 
        android:padding="5dp">

        <ImageView android:id="@+id/tab_icon" 
             android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:scaleType="fitCenter" />

        <TextView android:id="@+id/tab_text" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:singleLine="true"
            android:textStyle="bold" 
            android:gravity="center_horizontal"
            android:textSize="10sp" 
            android:ellipsize="marquee"
            android:textColor="@android:color/black" />
    </LinearLayout>

This should cover everything you need for a tab widget...

你需要使用@ + id / someid,或者使用tabActivity并通过getTabHost()获取tabhost

Use android:id="@android:id/tabhost" in the xml property of TabHost :

<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">


<!-- other code for tab widget and Frame -->

</TabHost>

and in the source file use :

TabHost tabHost = getTabHost();

to get the tabHost.

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@android:id/tabhost"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

       // Your Code Here.

   </TabHost>

This is your xml for tabs . For this

TabHost tabHost = getTabHost();

in code to get tabhost reference.

 TabSpec firstTabSpec = tabHost.newTabSpec("tid1")
                               .setIndicator("Update-1")
                               .setContent(new Intent(this, Keyboard.class));

Apply this for another tabs also.

  tabHost.addTab(firstTabSpec);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM