Searchview does not work because the application is compatible

Since I deployed the application, my search query no longer works:

Process: com.laurenswuyts.witpa, PID: 26666 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference at com.laurenswuyts.witpa.Activities.Events.EventActivity.onCreateOptionsMenu(EventActivity.java:75) at android.app.Activity.onCreatePanelMenu(Activity.java:2820) at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275) at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276) at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79) at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49) at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459) at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

So the nullpointer for searchview so far I have:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.event_main, menu); // Get the SearchView and set the searchable configuration // Associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return super.onCreateOptionsMenu(menu); } 

And in my menu I have the following:

 <!-- Search Widget --> <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_action_search" app:showAsAction="always" android:actionViewClass="android.support.v7.widget.SearchView"/> 

I have no idea why it is no longer working, but this has happened since I started using the compat 21 application.

Hello,

+62
java android search appcompat menu android-appcompat
Nov 20 '14 at 20:49
source share
9 answers

Try using your own app namespace for your actionViewClass too:

 app:actionViewClass="android.support.v7.widget.SearchView"/> 
+230
Nov 20 '14 at 20:53
source share

Add the following line to the proguard-rules.pro file located inside the app folder:

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

+40
Jan 27 '17 at 19:31 on
source share

This can also happen if you have proguard protection enabled and it shares the aways SearchView class. You will need to change the proguard settings to save the class.

See this for more details.

+15
Feb 08 '15 at 6:54
source share

This is additional information for Simas answer. I found this in a different answer ( https://stackoverflow.com/a/3/9/ ), and this was very important for solving my exception:

Please note that this is app:actionViewClass NOT android:actionViewClass

+11
Nov 13 '15 at 3:59
source share

after some time playing "run and error" I found a solution. It seems that the user interface is not the cause of the error. After setting the search to QueryListener, it works well. Here is the code:

Activity that contains SearchBar:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { Log.d(TAG, "onQueryTextSubmit "); return false; } @Override public boolean onQueryTextChange(String s) { Log.d(TAG, "onQueryTextChange "); return false; } }); return true; } 

searchable.xml

 <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/search_hint"/> 

And the "SearchBar" in menu.xml

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

And last but not least, the manifest (but that should be clear) ...

  <activity android:name=".activities.MainActivity" android:label="@string/title_activity_main"> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity> 

Additional information → Documents

+10
Mar 03 '15 at 13:01
source share

If you use progurd, then you need to add the following line to the file proguard-rules.pro

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

or

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

+4
Jan 18 '18 at 20:29
source share

If you minimize the types of your assemblies, then you need to add one line to your file 'proguard-rules.pro'.

 buildTypes { release { debuggable false minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { debuggable true minifyEnabled false shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } 

Add a line to your file 'proguard-rules.pro'.

 -keep class android.support.v7.widget.SearchView { *; } 
+1
Oct. 20 '18 at 23:17
source share

After you click on the result, your application expects the operation to not complete and tries to jump to the Intent argument.

SearchView.OnSuggestionListener and return true , which notifies your application of the completion of the click operation.

  searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { @Override public boolean onSuggestionClick(int position) { return true; } @Override public boolean onSuggestionSelect(int position) { return false; } }); 
0
Mar 21 '17 at 10:11
source share

The problem occurs if Proguard is turned on. can be fixed by adding this to the proguard rules

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

0
Jan 31 '19 at 6:20
source share



All Articles