0

In the log, I have

NullPointerException : Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference.

Here is my following code :

public class MatchesActivity extends Activity implements ActionBar.OnNavigationListener {

    private ActionBar actionBar;
    private ArrayList<SpinnerNavItem> navSpinner;
    private TitleNavigationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matches);
        actionBar=getActionBar();

        actionBar.setDisplayShowTitleEnabled(false);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        navSpinner = new ArrayList<SpinnerNavItem>();
        navSpinner.add(new SpinnerNavItem("Botola Pro",R.drawable.ic_menu_camera));
        navSpinner.add(new SpinnerNavItem("Coupe du trone",R.drawable.ic_menu_camera));

        adapter = new TitleNavigationAdapter(getApplicationContext(),navSpinner);

        actionBar.setListNavigationCallbacks(adapter,this);

    }
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId){
        return false;
    }
}  

here is my styles.xml and my stylesv21.xml :

  <resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>
<resources>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Debug your code and find where you have the NullPointerException. It's the object actionBar that wasn't initialized. Here's a solution to your case : http://stackoverflow.com/questions/10031180/getactionbar-returns-null – Mathemagician Aug 11 '16 at 15:30
  • I have the same error !! – BENSEGHIR Mohamed Amine Aug 11 '16 at 15:46
  • here is the log :java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmohamedamine.moroccanfootballapp/com.example.bmohamedamine.moroccanfootballapp.MatchesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference – BENSEGHIR Mohamed Amine Aug 11 '16 at 15:46
  • Please post your styles XML that defines your theme so we can see the parent theme. You might have a theme that doesn't have an action bar. – kris larson Aug 11 '16 at 15:56
  • 1
    It's because you use `Activity` but your theme is `AppCompat`. You should change you should subclass `AppCompatActivity` and use `getSupportActionBar` instead. – Masked Man Aug 11 '16 at 16:06
  • @MaskedMan The `AppTheme.NoActionBar` is not an `AppCompat` theme – OneCricketeer Aug 11 '16 at 16:08
  • The problem is `windowActionBar` is set to `false`, so `getActionBar()` returns null – OneCricketeer Aug 11 '16 at 16:08
  • @cricket_007, it's using ` – Masked Man Aug 11 '16 at 16:12
  • @MaskedMan We don't know `AppTheme` is the theme being used for the `MatchesActivity` if it were, though, then you would see the error that says something like "activity must use AppCompat theme, or descendant" – OneCricketeer Aug 11 '16 at 16:14
  • @cricket_007 For the supporting actionBar, the method setNavigationMode() won't work ! – BENSEGHIR Mohamed Amine Aug 11 '16 at 16:18
  • The default theme in every `Activity` is the `App`'s theme unless it is overridden and you did my comment in your answer. – Masked Man Aug 11 '16 at 16:32

1 Answers1

0

To get you started, here's some pointers. Toolbar is the new ActionBar. You can define any custom Toolbar layout you want from XML.

For a Spinner, for example, save this as toolbar_spinner.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
     >

    <Spinner
        android:id="@+id/spinner_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

Now, you can simply include that Toolbar layout into your Activity. (Make sure that Activity uses a Theme without a Toolbar like Theme.AppCompat.NoActionBar)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_spinner" />

   <!-- Activity content here -->

</LinearLayout>

Then, you can start off your Activity class like so

public class SpinToolbarActivity extends AppCompatActivity {

    private Toolbar mToolbar;

    private Spinner mSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spintoolbar);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mSpinner = (Spinner) findViewById(R.id.spinner_nav);

        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);    
        }

        // TODO: add items to mSpinner using an Adapter

     }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245