Android - Nullpointerexception on Searchview in Action Bar

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>

Android - Null pointer exception in searchview

You should look for the search view in your fragment's view, not in your activity's view.

Change

searchView1 = (SearchView) getActivity().findViewById(R.id.searchView1);

to

searchView1 = (SearchView) rootView.findViewById(R.id.searchView1);

Android Action Bar SearchView NullPointerException adjustDropDownSizeAndPosition

This problem is caused by styles being applied to the EditText in the action bar SearchView.

You can fix this by removing any style you have applied to EditText such as:

res/values/styles.xml

<item name="android:editTextStyle">@style/editor</item>

Then go to each EditText view in your layout XML and apply the style directly:

<EditText style="@style/editor" />


Related Topics



Leave a reply



Submit