6

I'm following the tutorial https://www.youtube.com/watch?v=VVahIc8yENk and I'm getting the error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference

I'm using Android Studio to write this program and I have tried from API 11 to 21 and none of them work.

public class Tabtest extends FragmentActivity implements ActionBar.TabListener {

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

        actionBar=getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("tab1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2=actionBar.newTab();
        tab2.setText("tab2");
        tab2.setTabListener(this);

        ActionBar.Tab tab3=actionBar.newTab();
        tab3.setText("tab3");
        tab3.setTabListener(this);

        ActionBar.Tab tab4=actionBar.newTab();
        tab4.setText("tab4");
        tab4.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
        actionBar.addTab(tab4);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }
}
trooper
  • 4,444
  • 5
  • 32
  • 32
RashaadBryan
  • 55
  • 1
  • 1
  • 6
  • Where's the code for `getActionBar()`? – Jeff B Dec 31 '14 at 22:10
  • This answer may be helpful to you. http://stackoverflow.com/questions/26480626/appcompat-v21-material-design-actionbar-inflateexception-error-inflating-clas – Shami Jan 02 '15 at 01:29

4 Answers4

10

I was following Vivz example in youtube but when the method deprecated i had to find another way. Instead of adding tabs to the actionbar try:

Modify your adapter:

public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

    private String[] titles = {"Item 1", "Item 2", "Item 3" };

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch(i){
          case 0:
             return new FragmentA();
          case 1:
             return new FragmentB();
          case 2:
             return new FragmentC();        
        }
        return null;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

And in the activity that you would like to implement the tabs try

public class Tabtest extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_test);

    ViewPager pager = (ViewPager) findViewById(R.id.your_view_pager);
    pager.setAdapter(new CollectionPagerAdapter(getSupportFragmentManager()));

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
}

Now if you would like to style your tabs like Google Play store with a moving indicator under the tab name and move while the user scrolls compile this library

compile 'com.astuetz:pagerslidingtabstrip:1.0.1'

And modify your viewpager layout file like this:

<LinearLayout
  //obviously add width and height and other necessery stuff
  android:orientation="vertical">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


</LinearLayout>

And then you are going to have the desired effect.

Hope it helps!!!

4

you can use "PagerSlidingTabStrip" replacing "setNavigationMode"

here a tutorial and here a example in Github

David Hackro
  • 3,652
  • 6
  • 41
  • 61
1

finally i found answer. 1st your android sdk

<uses-sdk android:minSdkVersion="11" />

it must higher than 11. 2nd your theme must have actionbar like

android:theme="Theme.AppCompat"

3rd don't use this code in your activity

requestWindowFeature(Window.FEATURE_NO_TITLE);

4th instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity

5th change this actionBar=getActionBar(); to actionbar=getSupportActionBar();

Hojjat Mehri
  • 188
  • 1
  • 11
0
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference

Your theme apparently is one that does not have an action bar. Since you are not using appcompat-v7, make sure that you are using a theme that supports the native action bar, such as one of the Theme.Holo series.

If, instead, you are trying to use appcompat-v7, you need to inherit from ActionBarActivity and you need to use getSupportActionBar(), not getActionBar().

Also note that action bar tabs were deprecated as of Android 5.0. You should consider another tab solution, such as the combination of ViewPager and PagerTabStrip.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • when I made the project i saw nothing about a theme so how do i select one ? and also could you send me a tutorial for the combination of ViewPager and PagerTabStrip – RashaadBryan Dec 31 '14 at 22:32
  • @RashaadBryan: "so how do i select one ?" -- look for `android:theme` attributes in your manifest, in the `` and/or `` elements. – CommonsWare Dec 31 '14 at 22:37
  • @RashaadBryan: With regards to `ViewPager` and `PagerTitleStrip`, here is a sample app from my book: https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Indicator There are probably some tutorials floating around that you might find via a search engine. – CommonsWare Dec 31 '14 at 22:38
  • i check under styles and i have the theme – RashaadBryan Dec 31 '14 at 22:43
  • @RashaadBryan: If you are going to use something based on `Theme.AppCompat`, you need to inherit from `ActionBarActivity` and use `getSupportActionBar()`, not `getActionBar()`. – CommonsWare Dec 31 '14 at 23:01
  • i tried that put i still get the same problem setNavigationMode is deprecated @CommonsWare – RashaadBryan Dec 31 '14 at 23:16
  • @RashaadBryan: "setNavigationMode is deprecated" -- yes, I mentioned that at the end of my answer. There is nothing that you can do to make it be not deprecated, unless you can go back in time. Your choices are either to live with the deprecation warning or use another tab solution. – CommonsWare Dec 31 '14 at 23:18