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)
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.
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 ^^
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
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/
Instead of
return true;
add this line
return super.onCreateOptionsMenu(menu);
Related
Activity
with #import android.support.v7.widget.SearchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
search_menu 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"
xmlns:yourapp="http://schemas.android.com/tools">
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="search"
app:showAsAction="collapseActionView|always"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
when i use support search view it throws an null pointer
java.lang.NullPointerException
at .MainActivity.onCreateOptionsMenu(MainActivity.java:42)
at android.app.Activity.onCreatePanelMenu(Activity.java:2585)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:230)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
but when i change the xml to search widget instead of support widget as follows and change the import on activity
#import android.widget.SearchView;
all works fine
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/tools">
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView" />
null poitner shows on this line if it helps
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
i have checked the available suggested solutions ..but none of them works..some soulutions are deprecated now
After extending AppCompatActivity instead of Activity, my project is giving the following error:
java.lang.NullPointerException: Attempt to invoke virtual method
'void
android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)'
on a null object reference
and I followed this link[ this][1] but it did not work for me.
Please give me some suggestions on what I am doing wrong.
This is my code for search:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
and this is menu xml
<item
android:id="#+id/search"
android:app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:app:showAsAction="collapseActionView|always"
android:title="#string/Search"/>
Try using the custom app namespace for your actionViewClass too:
app:actionViewClass="android.support.v7.widget.SearchView"/>
You have to change actionview class of searchview to support and change schema in menu. 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/search"
android:title="Search"
android:titleCondensed="false"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="0"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Then in the code you have to get the searchview and use expand listent like this. Remember to use SearchView of support library.
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setQueryHint(mContext.getString(R.string.search_messages));
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
}
return true;
}
});
In my app I have a SearchView that uses the support.v7 library because of compatibility.
The problem is that, in a device with API 11 or higher, when I open the searchview i get the support api style and only after I search for something the style changes to my application style.
I want the theme in the newest APIs to be always like the second image. Already tried other themes but the result is always the same.
Style when the searchview expands:image link.
Style after a search:image link
options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:malfriends="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"
malfriends:actionViewClass="android.support.v7.widget.SearchView"
malfriends:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="#+id/logoff"
android:title="#string/logout"
malfriends:showAsAction="never"/>
</menu>
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:searchSuggestAuthority=
"com.dandrex.malfriends.controller.provider.SearchFriendsProvider"
android:searchSuggestSelection=" ?" />
My activity class
import android.support.v7.widget.SearchView;
...
public class FeedTabActivity extends ActionBarActivity implements
ActionBar.TabListener
onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(new ComponentName(
FeedTabActivity.this, FeedTabActivity.class)));
searchView.setIconifiedByDefault(false);
} else {
MenuItem itemCompat = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(itemCompat);
}
return true;
}
The problem occurs when interacting with the SearchView or when the activity with the Searchview loads. When using setIconifiedByDefault(true) the problem occurs after the activity loads when interacting with the SearchView, but when using setIconifiedByDefault(false) the problem occurs when the activity loads.
The following error is output in LogCat:
java.lang.NullPointerException
at android.widget.SearchView.adjustDropDownSizeAndPosition(SearchView.java:1244)
Here is the code:
ExampleActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(true);
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Perform search
return true;
}
});
}
return true;
}
menu/example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search"
android:title="Search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
xml/searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search"
android:hint="#string/search" >
</searchable>
The "search" string used in searchable.xml is defined in values/strings.xml. Also the searchable.xml file is referenced correctly in the activity tag in AndroidManifest.xml:
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
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" />
I would like to create an always-expanded search window in my action bar. I am using the ActionBar Sherlock library. According to some samples I have found, this code should do the trick
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
SearchView searchView = (SearchView) searchItem.getActionView(); //returns null
searchView.setIconifiedByDefault(false);
return true;
}
however, getActionView(); returns null.
Is there some other way to get to the searchView, so that I can call the setIconifiedByDefault(false); method ?
I would like to keep the definition of the searchView in the xml file as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_bar_search"
android:title="#string/action_bar_search"
android:icon="#drawable/ic_magnify"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Thanks for any help
Your issue is probably with your XML. Change android:actionViewClass="android.widget.SearchView" to android:actionViewClass="com.actionbarsherlock.widget.SearchView"
Are you sure that you're using correct R.menu... link? Also try to use getMenuInflater(); instead of getSupportMenuInflater();