Android - NullPointerException on SearchView in Action Bar Android - NullPointerException on SearchView in Action Bar android android

Android - NullPointerException on SearchView in Action Bar


Try to replace the failing line with:

mSearchMenuItem = menu.findItem(R.id.action_search);mSearchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(mSearchMenuItem);

Where R.id.action_search is the id of your search item in the menu.

EDIT

Your manifest should look like that:

<activity       android:name="com.bronzelabs.twc.activities.WorkflowListActivity"       android:label="@string/app_name"       android:theme="@style/Theme.AppCompat.Light" >        <intent-filter>            <action android:name="android.intent.action.MAIN" />            <category android:name="android.intent.category.LAUNCHER" />        </intent-filter></activity><activity         android:name=".activities.SearchResultActivity"        android:theme="@style/Theme.AppCompat.Light"        android:launchMode="singleTop">        <intent-filter>            <action android:name="android.intent.action.SEARCH" />        </intent-filter>        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"            android:value=".activities.SearchResultActivity" />    </activity>

The way you call setSearchableInfo is:

mSearchView.setSearchableInfo(searchManager.getSearchableInfo(            new ComponentName(getApplicationContext(), SearchResultActivity.class)));

EDIT 2

Make sure your menu xml file is like that (pay attention to those 2 attributes with yourapp namespace - this is needed when you use the compat library):

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" >    <item android:id="@+id/action_search"      android:title="@string/action_search"      android:icon="@drawable/action_search"      yourapp:showAsAction="always|collapseActionView"      yourapp:actionViewClass="android.support.v7.widget.SearchView" /></menu>


I had a similar probably that appeared on my release builds but not my debug builds when switching over to the v21 support library. Turned out to be an obfuscation problem, and adding this line to my proguard-rules.txt file fixed it:

-keep class android.support.v7.widget.SearchView { *; }


First check you are using app namespace for actionViewClass.

android:actionViewClass="android.support.v7.widget.SearchView"-this throws NPE

app:actionViewClass="android.support.v7.widget.SearchView"-use this

and second

Check if you are using same searchView in menu.xml and in your Activity