0

I wanted to implement tabs and a tablistener to my app.

    public class MyActivity extends Activity {

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

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

        ActionBar.Tab tabA = actionBar.newTab();
        tabA.setText("Tab A");
        tabA.setTabListener(new TabListener<Tab1>(this, "Tag A", Tab1.class));
        actionBar.addTab(tabA);

        Tab tabB = actionBar.newTab();
        tabB.setText("Tab B");
        tabB.setTabListener(new TabListener<Tab2>(this, "Tag B", Tab2.class));
        actionBar.addTab(tabB);

        Tab tabC = actionBar.newTab();
        tabC.setText("Tab C");
        tabC.setTabListener(new TabListener<Tab3>(this, "Tag C", Tab3.class));
        actionBar.addTab(tabC);

        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
    }

}

But i get an NullPointerException from this line:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

Could somebody tell me why and what to do?

I will add the other classes and the StackTrace if needed.

adneal
  • 30,484
  • 10
  • 122
  • 151
Fynn
  • 210
  • 1
  • 6
  • 28

2 Answers2

2

getActionBar() returns null. Do check the manifest if you applied a non action bar theme. If so change the theme so that the activity has actiobar.

public ActionBar getActionBar ()

Added in API level 11 Retrieve a reference to this activity's ActionBar.

Returns The Activity's ActionBar, or null if it does not have one

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Do you mean this code? I tried removing .NoTitleBar, but my app still crashes after that and i get the same nullpointerexception – Fynn Mar 31 '14 at 18:10
  • @user2051194 `android:theme="@android:style/Theme.Black.NoTitleBar"` is the problem. Apply a theme that has actionbar for the activity – Raghunandan Mar 31 '14 at 18:11
1

Which theme are you using? Check if theme is Theme.NoTitleBar in Manifest. If so, change it to i.e. Theme.Holo.

vanste25
  • 1,754
  • 14
  • 39
  • 1
    It works after i changed it to Theme.Holo Thank you very much! :) – Fynn Mar 31 '14 at 18:12
  • Is it possible to hide the apps name+icon with this theme? – Fynn Mar 31 '14 at 18:13
  • @user2051194 yes hide the icon and title in actiobar. http://stackoverflow.com/questions/5720715/remove-application-icon-and-title-from-honeycomb-action-bar – Raghunandan Mar 31 '14 at 18:13
  • 1
    You welcome. Use this methods: actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayShowHomeEnabled(false); – vanste25 Mar 31 '14 at 18:15