onCreateOptionsMenu () method throws null pointer when support search widget is used - android

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

Related

Material SearchView implementation error

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

Searchview in android Toolbar

I am trying to build a searchview in the toolbar ion my android app, but am getting a null pointer exception in the 3rd line of onCreateoptionsMenu method. Any help will be appreciated, thank you in advance.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
Here is my menu.xml -
<item
android:icon="#drawable/ic_search"
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
app:showAsAction="ifRoom" />
Set SearchView class in menu.
<item
android:icon="#drawable/ic_search"
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
You missd app:actionViewClass="android.support.v7.widget.SearchView" attribute.
And you need to import android.support.v7.widget.SearchView;
<?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/action_search"
android:icon="#drawable/search"
android:title="#string/Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
You forgot to add actionViewClass in menu.xml
Add these line to you menu.xml
...
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" />
While in your Java code find SearchView using -
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Reference - MenuItemCompat
Try to use this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
Try changing this line:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

Regards search view in android

I implemented search view for filtering items in listview. It's working fine. My problem is search view showing > ( next icon ) in action bar. What is this? how can i remove this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
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">
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>

Wrong menuitem found when fragment changes in Android

I have the following menu in the main fragment that contains a list:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
app:actionViewClass="android.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
It has the necessary setting in the onCreate:
setHasOptionsMenu(true);
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
if(searchView!=null)
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
}
When I click an item, a new fragment opens and it has a different menu:
<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=".MainActivity">
<item android:id="#+id/action_Save"
android:title="#string/action_Save"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_save"
/>
</menu>
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_fragment_second, menu);
super.onCreateOptionsMenu(menu, inflater);
If I press Back:
if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
my first fragment opens and tries to find its SearchView.
It can't find it as the menu of the second fragment is checked and it finds only the Save item.
What should I do to make it work?
You need to call invalidateOptionsMenu() to reset your menu for the fragment.
I had to change how the menuitem was found.
Instead of this:
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
I use now this:
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
This way it works.

Android: menu displaying at screen bottom

I was trying to create a menu, and add some actions "ifRoom" using code below:
<?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="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/search_description" />
<item
android:id="#+id/menu_refresh"
android:title="#string/menu_refresh"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/menu_preference"
android:title="#string/menu_preferences"
android:showAsAction="never"
/>
</menu>
But what i got is the menu is displaying at the bottom...
I want it to be at top of the screen...
Any idea what happened here? Thanks!
Screenshot link: http://i.stack.imgur.com/knECg.png
Below is the MainActivity.java onCreateOptionaMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Moved from onCreate -- Retrieve the Search View and configure/enable it
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
return true;
}

Categories

Resources