0

this question is really wierd and driving me crazy. i can't get rid of the nullPointerException. my task is quite simple, just run a test on the tabhost. but now i'm not able to even create one tabhost! in order to ceate a tabhost, there some points i need to be careful with. first there must be a tabwidget with an id of "@android:id/tabs" and a framelayout of which the id is "@android:id/tabconent". i swear i do pay attention and you can see that in my codes below. and i read my articles recommanding using TabActivity. but unfortunately this tabhost will be a component of an activity. so i have to create it in a normal activity.

when the code goes to "addSpec()", it throws a nullPointerException. i examine both the tabHost and Host.Spec in the Logcat. neither of them is null! i will add my print-out result to the end of this question.

here is my java code.

public class Cao extends Activity {
ScrollView menu_scroll;
TabHost menu_host;
TabWidget menu_tags;
int[] contents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testtab);
    menu_host = (TabHost) findViewById(R.id.tabhost);
    Log.e("tabhost", (menu_host == null) + "");
    menu_tags = (TabWidget) findViewById(android.R.id.tabs);
    contents = new int[] { R.id.b1, R.id.b2 };
    int count = 6;
    for (int i = 0; i < count; i++) {
        TabSpec spec = menu_host.newTabSpec(i + "");
        spec.setIndicator("no " + i);
        Log.e("spec", (spec == null) + "");
        spec.setContent(R.id.b1); **// the problem occurs at this line**
        menu_host.addTab(spec);
    }
}
}

and my xml code is as follows

<?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/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <HorizontalScrollView
            android:id="@+id/menu_scroll"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:scrollbars="none" >

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

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

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

    </FrameLayout>
</TabHost>

</LinearLayout>

in addition the logcat print-out

tabhost(490): false
spec(490): false
E/AndroidRuntime(490): Uncaught handler: thread main exiting due to uncaught exception  
E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.wirelessorder/com.tim.wirelessorder.ui.Cao}: java.lang.NullPointerException   
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(490):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(490): Caused by: java.lang.NullPointerException    
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:583)
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
E/AndroidRuntime(490):  at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
E/AndroidRuntime(490):  at com.tim.wirelessorder.ui.Cao.onCreate(Cao.java:38)
E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
ChinaMan
  • 39
  • 2
  • 12

3 Answers3

1

you need to extend TabActivity not simple activity try

public class Cao extends TabActivity 

intead of

public class Cao extends Activity 

and change this

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

to menu_host =getTabHost(); and

 android:id="@+id/tabhost"

to

android:id="@android:id/tabhost"
Akram
  • 7,548
  • 8
  • 45
  • 72
  • it is not nacessary to use TabActivity. i finally find the answer in this link http://stackoverflow.com/questions/3163884/android-tabhost-without-tabactivity thank you all the same. – ChinaMan May 26 '12 at 03:29
  • you never said you need to create without extending tabactivity anyway good to know your problem is solved – Akram May 26 '12 at 03:50
1

//change your tabhost id to

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

//your activity which extends TabActivity like this below one

public class HelloTabWidget extends TabActivity {

//in your onCreate do this

 TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

check this:

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • sorry, i don't want to use TabHost. case this activity already has a superclass. i've fixxed the problem. hope this answer could help others http://stackoverflow.com/questions/3163884/android-tabhost-without-tabactivity – ChinaMan May 26 '12 at 03:33
0

All that was needed for me was to change the way I declared the ID of the TabHost in .xml. Rather than using "@+id/tabhost", I used "@android:id/tabhost". Then, when assigning my TabHost in code, I called findViewById(android.R.id.tabhost). This, for whatever reason, fixed my problem.

brainmurphy1
  • 1,082
  • 1
  • 19
  • 24