0

Iam getting Nullpointer exception when adding tabspec to tabhost.i have tried like this

public class MainActivity extends FragmentActivity implements TabHost.TabContentFactory {

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

        TabHost tabHost= (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        TabSpec TextTabSpec = tabHost.newTabSpec("Text");
        TextTabSpec.setIndicator("", getResources().getDrawable(R.drawable.phone_1));
        TextTabSpec.setContent(this);

        TabSpec LogTabSpec = tabHost.newTabSpec("Favourites");
        LogTabSpec.setIndicator("", getResources().getDrawable(R.drawable.call_group));
        LogTabSpec.setContent(this);

        TabSpec ConTabSpec = tabHost.newTabSpec("contacts");
        ConTabSpec.setIndicator("", getResources().getDrawable(R.drawable.message));
        ConTabSpec.setContent(this);

        tabHost.addTab(TextTabSpec);
        tabHost.addTab(LogTabSpec);
        tabHost.addTab(ConTabSpec);
        tabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public View createTabContent(String tag) {
        // TODO Auto-generated method stub
        return null;
    }

}

an my logcat is

05-16 11:08:29.650: E/AndroidRuntime(655): FATAL EXCEPTION: main
05-16 11:08:29.650: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabsample/com.example.tabsample.MainActivity}: java.lang.NullPointerException
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.os.Looper.loop(Looper.java:123)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 11:08:29.650: E/AndroidRuntime(655):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 11:08:29.650: E/AndroidRuntime(655):  at java.lang.reflect.Method.invoke(Method.java:521)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 11:08:29.650: E/AndroidRuntime(655):  at dalvik.system.NativeStart.main(Native Method)
05-16 11:08:29.650: E/AndroidRuntime(655): Caused by: java.lang.NullPointerException
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:622)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost.addTab(TabHost.java:213)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.example.tabsample.MainActivity.onCreate(MainActivity.java:33)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-16 11:08:29.650: E/AndroidRuntime(655):  ... 11 more

iam not getting where i have done the mistake.please anyone help me to solve this..

2 Answers 2

1

It is better to use this one.

TabHost tabhost=(TabHost)findViewById(R.id.tabhost);
        tabhost.setup();
        TabHost.TabSpec tabsep=tabhost.newTabSpec("tag1");  // this one needed
        tabsep.setContent(R.id.tab1);
        tabsep.setIndicator("Clock");
        tabhost.addTab(tabsep);

        tabsep=tabhost.newTabSpec("tag2");
        tabsep.setContent(R.id.tab2);
        tabsep.setIndicator("Button");
        tabhost.addTab(tabsep);
        tabhost.setCurrentTab(0);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for responding.but no change if i use like that also
extends super class Activity in place of FragmentActivity implements TabHost.TabContentFactory. i hope it will work
Please refrain from posting new answer as new post, you can always edit your post to add another solution.
0

extends the super class TabActivity and get TabHost tabHost=getTabHost(); like that

public class TabHost_Activity extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host_);

      Resources resource=getResources();
     TabHost tabHost=getTabHost();
TabSpec tabSpecAndroid = tabHost.newTabSpec("Android").setIndicator("", resource.getDrawable(R.drawable.icon_android_config)).setContent(this);

tabHost.addTab(tabSpecAndroid);
}
}

1 Comment

I have a doubt.is TabActivity not been deprecated.Eclipse raised a warning when I tried

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.