neo
neo

Reputation: 177

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabHost.setup()' on a null object reference

I've added a tabwidget in one of my activities. Just trying to get it to render. However, app keeps crashing with the above null pointer error. Getting error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void  android.widget.TabHost.setup()' on a null object reference

Here is my activity class:

import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TabHost.TabSpec;

public class DeleteAccountActivity extends Activity {


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

//tabhost
    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();
    TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabSpec tab3 = tabHost.newTabSpec("Third Tab");

    tab1.setIndicator("linearLayout");
    tab1.setContent(new Intent(this, test1.class));

    tab2.setIndicator("Tab2");
    tab2.setContent(new Intent(this, test2.class));

    tab3.setIndicator("Tab3");
    tab3.setContent(new Intent(this, test3.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);

}

Upvotes: 1

Views: 3863

Answers (2)

Navaneeth Pk
Navaneeth Pk

Reputation: 662

This will be solved if you use onCreateView instead of onCreate.

Upvotes: 0

Jan
Jan

Reputation: 1504

Try changing (TabHost)findViewById(android.R.id.tabhost); to (TabHost)findViewById(R.id.tabhost);

If that doesn't work, could you post your xml file?

Upvotes: 2

Related Questions