kaynewilder
kaynewilder

Reputation: 853

getActionBar() returns NULL in actionBar

I am currently creating the actionBar in android studio and I have created action buttons and my aim is to have a dropdown menu everytime I click a button.

In my menu_main.xml, I added the items search and help:

        <item
        android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom"/>

        <item android:id="@+id/action_help"
        android:icon="@drawable/ic_action_help"
        android:title="@string/action_help"
        app:showAsAction="ifRoom"/>

In my MainActivity.java, I have added the Strings for my dropdown:

    String[] actions = new String[] {
            "Bookmark", "Subscribe", "Share", "Like"
    };

In my onOptionsItemSelected() method, i have added the case where everytime you click on this button, it will call a function. Here is my code:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_help:
                helpMessage();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

        private void helpMessage() {
            //Toast.makeText(this, "Location button pressed", Toast.LENGTH_SHORT).show();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
            System.out.println("HERE: " + getActionBar());
            getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            ActionBar.OnNavigationListener navigationListener = new ActionBar.OnNavigationListener() {
                @Override
                public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                    Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
                    return false;
                }
            };
            getActionBar().setListNavigationCallbacks(adapter, navigationListener);
        }

When I run the program, my app will crash and I get

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

It points to getActionBar() which is NULL. Any thoughts?

Upvotes: 0

Views: 1431

Answers (3)

MrTy
MrTy

Reputation: 140

I think you should 1.Check themes

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

2.Check your Activity class , it's extend AppcombatActivity or ActionBarActivity

So, I hope it useful for you

Upvotes: 0

Nic
Nic

Reputation: 1024

I always have this error.... I found a solution which I put this to my Style it will be work and getActionBar() won't be null anymore...

<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>

Hope this help you

Upvotes: 3

Ilya Vorobiev
Ilya Vorobiev

Reputation: 836

Use getSupportActionBar() instead of getActionBar(). Also theme of your activity should be extended of Theme.AppCompat.

Upvotes: 1

Related Questions