【问题标题】:tabHost.setup() gives null pointer exception (Android studio)tabHost.setup() 给出空指针异常(Android 工作室)
【发布时间】:2014-01-24 02:50:08
【问题描述】:

我有一个非常简单的应用程序,它只是一个带有标签视图的活动。

我已将所有内容初始化并转换为应有的状态,但我不断收到一个空指针错误,该错误总是链接回

tabHost.setup();

我正在使用 android studio 并且是 java 新手。这个问题在这里被问了很多,但所有的答案都只是说包括 setup() 而我已经这样做了。

这是我的 .java 文件:

package com.example.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.TabHost;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


    TabHost tabHost = (TabHost) 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);

}


@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 boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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

}

我的代码和一些在线教程的唯一区别是公共类 MainActivity 扩展到 ActionBarActivity 而不仅仅是 Activity。我什至没有这样做,这是我创建空白项目时的默认设置。

XML 文件也在下面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.app.MainActivity$PlaceholderFragment">

<TabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"></LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

【问题讨论】:

    标签: java android android-studio android-tabhost


    【解决方案1】:

    此行显示您创建的 TabHost 位于布局目录内的 fragment_main.xml 中,该目录由 PlaceholderFragment 使用

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

    但您在activity_main.xml 中找到您的 TabHost

    将你的代码从 onCreate() 移动到 PlaceholderFragment 的 onCreateView,如果你使用默认模板,则在下面的同一类中,就像 AS 一样

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

    或者,如果您愿意,您也可以将 TabHost 移动到 activity_main.xml,而无需更改 java 代码,但不建议使用具有很多好处的片段。

    查看这里以了解使用 Fragments 的好处

    fragment_main and activity_main layouts in Android Studio

    【讨论】:

    • 是的,但是干杯你不仅是这样做的人,还有很多用户报告了类似的问题:)
    【解决方案2】:

    这是我使用 TabHost 的代码,但现在我使用片段代替。

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

    【讨论】:

    • 我将 Intent 放入 setContent()。
    • 我宁愿不扩展 TabActivity,因为我在搜索我的问题时所阅读的内容是它限制了你。此外,这似乎是很多代码,我已经在我发布的多行中看到了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2014-09-06
    • 2014-07-19
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多