Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException on SearchView using AppCompat in Android 5.0

I am updating my app using all AppCompat themeing. I get a nullpointer here:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    setupNewSearchView(searchItem, searchManager);

    return true;
}

private void setupNewSearchView(final MenuItem searchItem,
                                SearchManager searchManager) {
    final SearchView searchView = (SearchView) searchItem.getActionView();

   // Null at line below
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

   // Everything else below edited out
}

Here are my key imports:

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;

Am I correct in thinking this line must change when updating to AppCompat?

final SearchView searchView = (SearchView) searchItem.getActionView();

UPDATE:

I changed it to this

   final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

With same null result...

UPDATE - ADD XML

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

     <item
        android:id="@+id/menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        android:title="search" />
    <item

</menu>

LogCat:

10-24 11:49:07.040: E/AndroidRuntime(4846): java.lang.NullPointerException
10-24 11:49:07.040: E/AndroidRuntime(4846):     at com.---.---.MainFragmentActivity.setupNewSearchView(MainFragmentActivity.java:1300)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at com.---.---.MainFragmentActivity.onCreateOptionsMenu(MainFragmentActivity.java:1292)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056)
10-24 11:49:07.040: E/AndroidRuntime(4846):     at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)
like image 314
TheLettuceMaster Avatar asked Oct 24 '14 18:10

TheLettuceMaster


1 Answers

With support library you have to use a speicific menu item class: MenuItemCompat.

android.support.v4.view.MenuItemCompat

The method name (getActionView) stays the same.

Your menu xml should look like this:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
</menu>
like image 79
Simas Avatar answered Sep 22 '22 16:09

Simas