0

I am trying to build Android Tab Layout with Swipeable Views using this tutorial.

My FragmentActivity is :

public class BookDetailsActivity extends FragmentActivity implements ActionBar.TabListener {
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = {"Description", "Find book"};
    static ArrayList<String> book;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_books_details);

        book = new ArrayList<String>();
        book = (ArrayList<String>) getIntent().getSerializableExtra("bookMap");

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), book, getApplicationContext());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    public static ArrayList<String> getRequiredData() {
        return book;
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

}

But i am getting an error in:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

The error says:

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

Why am i getting this error?

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Pial Kanti
  • 1,550
  • 2
  • 13
  • 26
  • 1
    use getSupportActionBar() instead – Pooya Feb 05 '16 at 18:40
  • 2
    Possible duplicate of [getActionBar() returns null](http://stackoverflow.com/questions/6867076/getactionbar-returns-null) – N J Feb 05 '16 at 18:47
  • Check this link: http://stackoverflow.com/questions/35203624/android-studio-actionbar-tablistener-does-not-work-with-appcompatactivity it is deprecated – ʍѳђઽ૯ท Feb 05 '16 at 19:03

3 Answers3

1

I agree with Pooya. getSupportActionBar() should work

FixXxeR
  • 169
  • 2
  • 2
  • 11
1

change getActionBar() to getSupportActionBar()

also change FragmentActivity to AppcompatActivity

JAAD
  • 12,349
  • 7
  • 36
  • 57
1

Android Studio: ActionBar.TabListener does not work with AppCompatActivity?

ActionBar.TabListener is a deprecated interface.I hope you will not use that in future.

And, ActionBarTabs can be replaced by:

Like i said before in my answer: https://stackoverflow.com/a/35203962/4409113

But for fixing the issue, That's because ActionBar was not enabled for the right theme and you are extending FragmentActivity.

Please check this link: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

Use AppCompatActivity instead to add ActionBar and if you used it, you will need this too:

ActionBar actionBar = getSupportActionBar();
Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108