tabHost.setup() gives null pointer exception (Android studio)
Asked Answered
U

2

0

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

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.

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

}

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.

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>

Upanddown answered 24/1, 2014 at 2:50 Comment(0)
W
2

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

Walz answered 24/1, 2014 at 4:40 Comment(1)
ya it was, but cheers you are not only the one who did this, there are many users reported similar issue :)Walz
B
-1

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);
}}
Burnout answered 24/1, 2014 at 2:59 Comment(2)
I put an Intent into setContent().Burnout
I would prefer to not extend TabActivity as what i've read searching for my problem is that it restricts you. Also this seems like a lot of code and I have seen it down in as many lines as i posted.Upanddown

© 2022 - 2024 — McMap. All rights reserved.