tabHost.setup() gives null pointer exception (Android studio)

落花浮王杯 提交于 2019-12-01 14:38:04
pyus13

This Line shows that the TabHost you have created is in fragment_main.xml inside layout directory which is used by PlaceholderFragment

tools:context="com.example.app.MainActivity$PlaceholderFragment"

But you are finding your TabHost in activity_main.xml

Move your code from onCreate() to onCreateView of PlaceholderFragment, below in same class if you are using default template comes with AS like

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabHost);
    tabHost.setup();

    TabHost.TabSpec spec1, spec2, spec3;

    spec1 = tabHost.newTabSpec("spec1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab1");
    tabHost.addTab(spec1);

    spec2 = tabHost.newTabSpec("spec2");
    spec2.setContent(R.id.tab2);
    spec2.setIndicator("Tab2");
    tabHost.addTab(spec2);

    spec3 = tabHost.newTabSpec("spec3");
    spec3.setContent(R.id.tab3);
    spec3.setIndicator("Tab3");
    tabHost.addTab(spec3);

   return rootView;
  }

Or If you want you can move your TabHost to activity_main.xml as well without changing the java code but that is not recommended using fragments having a lot of benefits.

Check this for benefits of using Fragments

fragment_main and activity_main layouts in Android Studio

This my code to use TabHost,but now I use fragment instead.

public class MainActivity extends TabActivity {
private TabHost tabHost;
private TabHost.TabSpec spec;
@SuppressWarnings("unused")
private Resources res;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main_new);
    //Test Button
    Button testBtn = (Button)findViewById(R.id.title_imagebtn);
    testBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            dialog();
        }
    });


    this.res = getResources();
    this.tabHost = getTabHost();

    Intent intent = new Intent().setClass(this, LearnResActivity.class);
    this.spec = this.tabHost
            .newTabSpec(getString(R.string.res_learn))
            .setIndicator(
                    getString(R.string.res_learn),
                    getResources().getDrawable(
                            android.R.drawable.ic_media_play))
            .setContent(intent);
    this.tabHost.addTab(this.spec);

    this.tabHost = getTabHost();

    intent = new Intent().setClass(this, MyLearnActivity.class);
    this.spec = this.tabHost
            .newTabSpec(getResources().getString(R.string.my_learn))
            .setIndicator(
                    getString(R.string.my_learn),
                    getResources().getDrawable(
                            android.R.drawable.ic_menu_recent_history))
            .setContent(intent);
    this.tabHost.addTab(this.spec);
    this.tabHost = getTabHost();

    intent = new Intent().setClass(this, MyTestActivity.class);
    this.spec = this.tabHost
            .newTabSpec(getString(R.string.my_test))
            .setIndicator(
                    getString(R.string.my_test),
                    getResources().getDrawable(
                            android.R.drawable.ic_menu_edit))
            .setContent(intent);
    this.tabHost.addTab(this.spec);
}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!