Null pointer exception in searchview - android

I am trying to create list search view in shreyaloclistfragment. I am getting NullPointerException at folowing line
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
My method is
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate( R.menu.menu_search, menu);
System.out.println( "inflating menu");
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
showFilteredItems( newText );
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return;
}
Stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sears.syw.FragmentShopsTab.onCreateOptionsMenu(FragmentShopsTab.java:52)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
at
com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559 )
at
com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSh erlockNative.java:65)
at

If you are using the Appcompat library, maybe you forgot to add your own xmlns on your menu_search.xml.
As the example says:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
You need to add the folowing:
<item android:id="+#id/..."
android:actionViewClass="android.widget.SearchView"
yourapp:actionViewClass="android.widget.SearchView" />
Source: https://developer.android.com/training/basics/actionbar/adding-buttons.html#XML

If you are using AppCompat library, then in your menu.xml, add
app:actionViewClass="android.support.v7.widget.SearchView"
instead of
app:actionViewClass="android.widget.SearchView"

i am using android studio
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.myapp" >
<item
android:id="#+id/search"
android:title="Buscar"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
app:actionViewClass="android.widget.SearchView"/>
</menu>
found 100%

To reference a layout from a menu xml use the actionLayout attribute:
<menu>
<item android:id="#+id/menu_name"
android:actionLayout="#layout/your_layout"
android:showAsAction="always"/>
</menu>

Some times problem is with
setHasOptionsMenu(true);
Move it from oncreate or onviewcreate to on attached method and try it.

Check this answer
Either you are using wrong namespace or importing wrong class
https://stackoverflow.com/a/38702262/5374951

Related

Searchview interfering with other buttons

I have a searchview next to a filter button, but ever time I click on the other button the searchview also listens and expands, here is a snippet of the before How the buttons are positioned
When filter button is clicked
and here is my menu layout:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
my Java code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activty_ecommerce_sort, menu);
SearchManager manager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
search.setActivated(true);
search.setQueryHint("Search");
search.onActionViewExpanded();
search.setIconified(false);
search.clearFocus();
search.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id){
case R.id.action_sort:
filterDailog();
break;
}
return super.onOptionsItemSelected(menuItem);
}
Anyone have an idea where the issue is?
You need to remove app:actionViewClass="android.support.v7.widget.SearchView" from sort item.
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:showAsAction="ifRoom|collapseActionView" />
instead of;
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />

Back button of Searchview

I'm using Toolbar and SearchView for the ActionBar. After submit query text, It's changed like the picture below. How to set OnClick to the back button?
Edit 1: Here are involve code
menu.xml
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#drawable/ic_search_white"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
In my Fragment class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(myActionMenuItem);
final ImageView mCloseButton = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(final String query) {
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
mCloseButton.performClick();
return false;
}
...
Simply add android:showAsAction="always|collapseActionView" in your menu item.
Example menu.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search_white_24dp"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>

Giving error on my searchbar after extending AppCompatActivity

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;
}
});

Android - Up and down navigation buttons on expanding the Search widget

I am building a chat application just like Whatsapp. I have build the search functionality successfully in my application using the SearchView in the ActionBar. This is what I have done so far:
Instead of this I want the SearchView with navigation up and down arrows (like in Whatsapp) when I click on the search button. This is what I want to achieve:
Here is my code for the Search button:
// SearchView
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
adapter.filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return true;
}
});
This is the xml file in my menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:showAsAction="never|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Please tell me how to achieve this. Thanks

menu.findItem returns null

I can't find the solution please help
The implements:
public class VentasActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, android.support.v7.widget.SearchView.OnQueryTextListener
where returns null:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.sales, menu);
restoreActionBar();
// HERE RETURNS NULL
searchView = (SearchView) menu.findItem(R.id.action_product_search).getActionView();
if (searchView != null) {
final Menu menu_block = menu;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// collapse the view ?
menu_block.findItem(R.id.action_product_search).collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
// listAdapter.getFilter().filter(query);
return false;
}
});
Log.i("sales module", "SearchView OK");
} else
Log.i("sales module", "SearchView is null");
return true;
}
return super.onCreateOptionsMenu(menu);
}
and sales.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_product_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
Many many thanks!
UPDATED: the code of restoreActionBar()
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
finally i found the problem:
I changed the last line in my /res/menu/sales.xml from:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="#+id/search_view_productos"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="#+id/search_view_productos"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
</menu>
Problem was the xml namespace on actionViewClass
for me cause of issue was wrong import:
android.widget.SearchView
instead:
androidx.appcompat.widget.SearchView
In my case, i was using vector image for "android:icon". I resolved it by using custom layout for menu item.
In my case, I added app:actionViewClass="android.widget.SearchView" attribute to my <item> tag

Categories

Resources