本文介绍了tabHost.setup() 给出空指针异常(Android studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序,它只是一个带有选项卡视图的活动.

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 null pointer error which always links back to

tabHost.setup();

tabHost.setup();

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

I am using android studio and am new to java. This question has been asked a lot on here but all answers just say to include the setup() and I have already done that.

这是我的 .java 文件:

Here is my .java file:

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.我什至没有这样做,这是我创建空白项目时的默认设置.

The only difference between my code and some online tutorials is the public class MainActivity extending to ActionBarActivity and not just Activity. I didn't even do this, it was default when i created a blank project.

XML 文件也在下面:

The XML file is below also:

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

推荐答案

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

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"

但是您正在 activity_main.xml

将您的代码从 onCreate() 移动到 PlaceholderFragment 的 onCreateView,如果您使用的是默认模板,则在同一个类下面

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

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

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.

检查使用 Fragment 的好处

Check this for benefits of using Fragments

Android Studio 中的fragment_main 和activity_main 布局

这篇关于tabHost.setup() 给出空指针异常(Android studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:27