Android Action Bar SearchView NullPointerException adjustDropDownSizeAndPosition - android

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" />

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

Failed to implement a search widget [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm trying to implement a search widget in my app . I found a useful tutorial from here.
My Activity A
But my app crashed.
Activity A
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.create_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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.add: // create new file
View menuItemView = findViewById(R.id.add);
PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Create New File".equals(item.getTitle())) {
Intent intent = new Intent(HomePage.this, Information.class); // go to Information class
startActivity(intent);
} else if ("Edit File".equals(item.getTitle())) {
Intent intent = new Intent(HomePage.this, Edit.class);
startActivity(intent);
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
searchable in xml folder
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search by date" />
create_menu
<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 by date"
android:icon="#mipmap/search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:icon="#mipmap/menu"
android:id="#+id/add"
android:orderInCategory="100"
android:title="Main Menu"
app:showAsAction="always" />
</menu>
declare this in mainfest
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
Error LogCat
01-18 18:31:09.298 9215-9215/com.example.project.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.myapplication, PID: 9215
java.lang.NullPointerException
at com.example.project.myapplication.GUI.HomePage.onCreateOptionsMenu(HomePage.java:104)
at android.app.Activity.onCreatePanelMenu(Activity.java:2646)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallback
This is the line where error pointed to searchView.setSearchableInfo(
You should use support SearchView's support version:
<item android:id="#+id/action_search"
android:title="#string/search_title"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
And you'd add in Manifest.xml the proper Intent Filter
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />

Search icon is getting displayed twice in Toolbar in Fragment

I am trying to implement a search functionality in my application.
I am trying to display search icon in toolbar, but instead of single search icon multiple icons are getting displayed. One icon is getting displayed from menu.xml file and other icon is getting displayed from the line setHasOptionsMenu(true);.
If I do not use "setHasOptionsMenu(true)" line then onOptionsItemSelectedMenu method will not get called, if I do not give search icon in menu.xml file then search icon will not get displayed. Please let me know how to come out of the issue. I am trying hard with no fruits. Please help me come out of this issue.
My current toolbar looks as shown in the image below:
My menu.xml file:
<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.blo.ifo.ifocusblogs.ActivityForFragments">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/search"
android:title="#string/search_label"
android:icon="#mipmap/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I had the same problem
my problem
and this was the solution that I've found:
add "menu.clear();" to "onPrepareOptionsMenu" in MainActivity
solution
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.main,menu);
MenuItem itemSearch = menu.findItem(R.id.action_search);
mSearchView = (SearchView) itemSearch.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
mSearchView.setQueryHint(getResources().getString(R.string.searchProduct));
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
adapter.getFilter().filter("");
listview.clearTextFilter();
}else {
adapter.getFilter().filter(newText.toString());
}
return true;
}
});
return true;
}

Change support SearchView theme

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

Problems with actionbar and searchview

I'm using actionBar and searchView, and this is my code:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedBundle) {
super.onCreate(savedBundle);
setContentView(R.layout.main_layout);
setupImageLoader();
setupSlidingMenu();
}
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
}
and my menu file is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Now what I see is this:
When not selected the SearchView is on the bottom of the screen, and it becames part of the actionbar only when active. How can I embed it like in Play store?
From your java-code, it seems that you are using FragmentActivity and thus the android-support library. But your selected SearchView-class is from the native Android framework.
Look at the developer-docs to see how it should be done:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
Also, it seems you have the splitActionbar enabled, or else your search-button would appear in the upper actionbar. So check your AndroidManifest.xml for something like this:
<activity uiOptions="splitActionBarWhenNarrow" ... >
<meta-data android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>

Categories

Resources