问题
I'm developing an android app, by now everything great, but when try to implement a Material SearchView with Google guidelines and following step by step some tutorials I can't figureout this error:
menu_main.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/search_ad"
android:enabled="true"
android:icon="@android:drawable/ic_menu_search"
android:title="Buscar"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="@+id/publish_ad"
android:enabled="true"
android:icon="@android:drawable/ic_menu_send"
android:title="Publicar anuncio"
android:visible="true"
app:showAsAction="never" />
<item
android:id="@+id/favs"
android:enabled="true"
android:title="Configuración"
android:visible="true"
app:showAsAction="never" />
</menu>
MainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_ad).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
error:
06-16 15:36:51.021 1239-1239/com.bachecubano.elbache E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bachecubano.elbache, PID: 1239
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.bachecubano.elbache.MainActivity.onCreateOptionsMenu(Unknown Source)
at android.app.Activity.onCreatePanelMenu(Activity.java:2889)
at android.support.v4.b.m.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.h$b.onCreatePanelMenu(Unknown Source)
at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
at android.support.v7.app.q.j(Unknown Source)
at android.support.v7.app.q$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
回答1:
Do you have added the following code on your manifest :
<activity
android:name=".ActivityWhereYouWantYourSearch">
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
And this on your res/xml folder (named searchable.xml) :
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name"
android:searchSettingsDescription="@string/search_description" />
I will also put a check on your onCreateOptionsMenu, like this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search_ad);
if (searchItem != null) {
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}
return true;
}
BTW, I don't use android:enabled and android:visible, but it's up to you to decide.
回答2:
I'm not familiar with this kind of SearchView but from the error I would guess that it can't get the ComponentName of the Activity. What I would try now is to put a this before getComponentName() --> this.getComponentName() to make sure where it should get the ComponentName.
Just a guess, so please don't hate me if that's wrong ^^
回答3:
You have to return true from onCreateOptionsMenu in order the inflation to take place. Therefore you get a NPE when trying to access menu before the return statement.
So remove those four lines of code to the appropriate method onPrepareOptionsMenu
回答4:
I guess the issue with your code is that you are using the latest version of support library but using the old style to access the "SearchView", that's why it's returning null. Use this line of code to access your searchview through "MenuItemCompat":
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Check this blog for more details:
https://chris.banes.me/2014/10/17/appcompat-v21/
回答5:
Instead of
return true;
add this line
return super.onCreateOptionsMenu(menu);
来源:https://stackoverflow.com/questions/44596788/material-searchview-implementation-error