1

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);

}

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.