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..

user1213202
  • 1,305
  • 11
  • 23

2 Answers2

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);
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
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);
}
}
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50