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

前端 未结 2 1285
失恋的感觉
失恋的感觉 2021-01-16 16:17

I have a very simple app which is just an activity with a tab view on it.

I have initialised and casted everything to as it should be but am continually getting a nu

2条回答
  •  梦毁少年i
    2021-01-16 17:03

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

提交回复
热议问题