tabHost.setup()给出空指针异常(安卓工作室) [英] tabHost.setup() gives null pointer exception (Android studio)

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

问题描述

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

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

我使用的机器人工作室是新来的Java。这个问题已经被问了很多在这里,但所有的答案,只是说包括设置(),我已经做到了。

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

}

之间我的code和一些在线教程,唯一的区别是公共类MainActivity延伸到ActionBarActivity而不仅仅是活动。我甚至没有做到这一点,这是默认的,当我创建了一个空白的项目。

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是 fragment_main.xml 这是用来布局目录中通过PlaceholderFragment

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"

但你发现你的TabHost在 activity_main.xml

移动你的code从的onCreate() onCreateView PlaceholderFragment的,低于同级别,如果你使用默认模板自带的像

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的code,但我们不建议使用具有很多碎片好处。

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.

选中此使用片段的好处

<一个href="http://stackoverflow.com/questions/21154148/fragment-main-and-activity-main-layouts-in-android-studio/21155124#21155124">fragment_main和activity_main布局在Android的工作室

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆