0

I am working with this tutorial to teach myself tab fragments. When I paste and run the MainActivity I get this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference at hss.fragmenttabstutorial.MainActivity.onCreate(MainActivity.java:27)

So I changed the Activity to ActionBarActivity, and changed the ActionBar to getSupportActionBar like many suggested. Now it won't build due to getSupportActionBar, stating "Incompatible types". What should I do?

Here's the Main code:

import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar;

import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new FragmentTab1();
Fragment toyotaFragmentTab = new FragmentTab2();
Fragment fordFragmentTab = new FragmentTab3();

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

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getSupportActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.
    bmwTab = actionBar.newTab().setText("Fragment1");
    toyotaTab = actionBar.newTab().setText("Fragment2");
    fordTab = actionBar.newTab().setText("Fragment3");

    // Setting tab listeners.
    bmwTab.setTabListener(new TabListener(bmwFragmentTab));
    toyotaTab.setTabListener(new TabListener(toyotaFragmentTab));
    fordTab.setTabListener(new TabListener(fordFragmentTab));

    // Adding tabs to the ActionBar.
    actionBar.addTab(bmwTab);
    actionBar.addTab(toyotaTab);
    actionBar.addTab(fordTab);
}

}

Kat
  • 2,460
  • 2
  • 36
  • 70
  • 1
    your actionbar import is wrong. you need to import the actionbar from the support package – njzk2 May 07 '15 at 19:04

1 Answers1

1

Instead of import android.app.ActionBar use android.support.v7.app.ActionBar.

This ensures compatibility with the rest of the support library including ActionBarActivity.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • So, when I change it to that, my TabListener class (i.e in ' toyotaTab.setTabListener(new TabListener(toyotaFragmentTab));' ) complains. I changed the library there too, but now it won't extend ActionBar.TabListener – Kat May 07 '15 at 19:08
  • 1
    Google seems to make it incredibly difficult for developers to adapt to new design styles and updates. I came across that problem too, and you will have to completely change how `ActionBar.TabListener` works. You could extend `AppCompatActivity` and change a lot of stuff, but for now, I would recommend just sticking to what you had before. If you want to use `AppCompatActivity`, it would mean you need to use `Toolbar` instead of `ActionBar`, and I answered a question [here](http://stackoverflow.com/questions/29868277/using-sliding-tabs-with-toolbar) on how to update tab layouts using that. – Farbod Salamat-Zadeh May 07 '15 at 21:05
  • Thanks! Good to know. Usually I resort to just saying "to hell with it!" and going back to the depreciated tabs. I'll look at your answer and see if it can help me. – Kat May 08 '15 at 13:56