0

I set

tabHost = (TabHost) findViewBiId(android.R.id.tabhost);

And it comes out NULL.

I have a MainActivity that extends ActionBarActivity and implements ActionBar.TabListener, as follows...

MainActivity

    public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;

        public static TabHost tabHost;

        private SharedPreferences mSharedPreferences;

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

        tabHost = (TabHost) findViewById(android.R.id.tabhost);

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    ... other methods ...
}

But when I try to use tabHost, I get a NullPointerException. Inspection of tabHost shows that it is, in fact, null.

Even a simple tabHost.getCurrentTab() crashes the whole thing. I would like to be able to switch tabs programmatically, but it isn't working. I've gone through dozens of threads and nothing seems to work out.

SectionsPagerAdapter

import java.util.Locale;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.TabHost;

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return new PunchCardFragment();
            case 1:
                return new CalendarFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return mContext.getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return mContext.getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "..."
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

activity_main xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>
Birrel
  • 4,754
  • 6
  • 38
  • 74
  • [http://stackoverflow.com/questions/18703962/actionbaractivity-with-tabhost](http://stackoverflow.com/questions/18703962/actionbaractivity-with-tabhost) – M D Feb 06 '15 at 07:28
  • [http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/](http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/) – M D Feb 06 '15 at 07:29
  • Post your activity_main.xml. – Lei Guo Feb 06 '15 at 07:31
  • Ok, I added the xml file. Any thoughts? ytRino posted an answer, but I'm not sure where to go with it. – Birrel Feb 06 '15 at 07:47

1 Answers1

0

Your activity_main.xml does not declare android:id="@+id/tabhost", just android:id="@+id/pager" with ViewPager.
findViewById gives you only views that set in setContentView(R.layout.activity_main)

ytRino
  • 1,450
  • 15
  • 28
  • What do I need to change to be able to access the tabs? I'd like to be able to find which tab I'm on, and be able to switch tabs. – Birrel Feb 06 '15 at 07:46
  • Use `PagerTabStrip` (found some tips https://gist.github.com/pboos/4535406 ) or 3rd library `ViewPagerIndicator` http://viewpagerindicator.com/ – ytRino Feb 06 '15 at 07:52
  • This is the stock code generated by the most current release of Android Studio when creating a new project (with the exception of moving SectionsPagerAdapter to its own class file). Why is this such a difficult task, for auto-generated code. – Birrel Feb 06 '15 at 08:00
  • i doubt that you edited some code. template project with ViewPager does not contain tabhost. – ytRino Feb 06 '15 at 08:06
  • I added that, in a attempt to access the tabs. Like I said, I need to know which one I am on, and be able to switch between them programmatically. – Birrel Feb 06 '15 at 08:07
  • `ViewPager` itself does not have title tab. Try above links then you can archive your desire ;) – ytRino Feb 06 '15 at 08:11