NullPointerException in Android code - android

The error is:
java.lang.NullPointerException.onCreateOptionsMenu(AddMyMarketsActivity.java:44)
My code is:
41 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
42 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
43 SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
44 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Log.i("searchView", "set searchable configuration for searchView ");
What could be the problem with my code?

If you're using androids support libary to display your action bar, be sure you use the right actionViewClass in your SearchView declaration.
Should be app:actionViewClass="android.support.v7.widget.SearchView".
After that you can use as replacement for your code trying to get the ActionView:
MenuItem searchItem = menu.findItem(R.id.search_view);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
Be sure to call getMenuInfalter().inflate(...) before calling this.
More information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

I had the same error and it was removed by changing:
android:actionViewClass="android.widget.SearchView"
to
app:actionViewClass="android.widget.SearchView"
from menu.xml file
i.e change android: to app:

I'm describing the whole procedure of adding search view to action bar.This snippets of code worked for me.
Add search view in menu:add the following item in the menu layout of the activity in which you require search view.
<item
android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
Note: menu layout file should contain following schema.
xmlns:app="http://schemas.android.com/apk/res-auto"
Create a Searchable Configuration:
create a file searchable.xml in the directory res/xml.
copy the following code into it.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
Modify onCreateOptionsMenu() method:
In the onCreateOptionsMenu() method of your activity, associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo):
Your onCreateOptionsMenu should look like this.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_cards, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView =
// (SearchView) menu.findItem(R.id.search).getActionView();
MenuItem mSearchMenuItem = menu.findItem(R.id.search);
SearchView searchView =
(SearchView)MenuItemCompat.getActionView(mSearchMenuItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(),
SearchResultsActivity.class)));
return true;
}
Make an activity SearchResultsActivity to show the results of your search.
In the manifest file ,add the following
<activity
android:name=".SearchResultsActivity"
android:label="#string/title_activity_search_results" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value=".SearchResultsActivity"/>
</activity>
Your SearchResultsActivity should implement the following methods
#Override
protected void onNewIntent(Intent intent) {
...
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data and show it in
//SearchResultsActivity
}
}
Note:You should call the following from onCreate method of SearchResultsActivity
handleIntent(getIntent());

Related

Search View (widget) broken when use backButton

I've created a search view using search widget, and when I open and search it works normally, the problem is when I go to any activity, including the searchview activity, and back using backbutton or finish() the activity, the search widget stops working. I mean, I still can type on widget, but the button to search(return) stops working, the searchable activity doesn't open anymore.
In the HomeActivity.java(main) I set up the search widget:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_settings, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).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
// Change Text color from search bar
final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
return true;
}
in Searchable.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null && toolbar != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setTitle("");
}
.
.
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
return true;
}
#Override
protected void onNewIntent(Intent intent) {
setIntent( intent );
handleSearch( getIntent() );
}
public void handleSearch(Intent intent){
if( Intent.ACTION_SEARCH.equalsIgnoreCase( intent.getAction() )){
String query = intent.getStringExtra(SearchManager.QUERY);
setTitle(query);
executeSearchShow(query);
}
}
So as I said, using backButton to back or calling finish() on activity the searchview widget stops working, but if I use homeUpButton it works normally.
I've override onBackPressed in Searchable activity and it works, but if I back from another activity(that I need to finish) to home the widget still stopping to work.
#Override
public void onBackPressed() {
onNavigateUp();
}
AndroidManifest.xml :
<activity
android:name=".activities.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.MySeries"
android:windowSoftInputMode="adjustPan|adjustNothing">
<meta-data
android:name="android.app.default_searchable"
android:value="br.com.adley.whatsnextseries.activities.Searchable"/>
</activity>
<activity android:name=".activities.Searchable"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>

Android toolbar SearchView is automatically opening keyboard

I am having a weird problem. In my MainActivity toolbar I have an action search and this is how I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
searchView.setIconified(false);
return true;
}
but the problem here is that when my app starts the keyboard pops up before I even click the search button in the toolbar. I tried to force the keyboard to hide in the manifest I added this to my MainActivity:
android:windowSoftInputMode="stateAlwaysHidden"
I even tried this:
android:windowSoftInputMode="stateHidden"
but they keyboard still pops up. I don't have any EditText or anything I only have an image and a FAB in my MainActivity.
1) android:windowSoftInputMode="adjustPan|stateHidden"
2) getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
3) mSearchView.clearFocus();

android collapsable searchview with custom header

I need the suggestion for using the best way to show searchview with custom header.
My headerlayout should initially look like this
And after clicking the search button on the right of the header, it must replace header title and menu icon part with the search input field like this.
pressing back in this state, should direct to initial state.
How can I do this?
in Menu Resource directory
search_country_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/action_search"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always|collapseActionView"
android:orderInCategory="0"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
**In Activity's onCreateOptionmenu Method **
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_country_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.action_search).expandActionView();
searchView.setQueryHint(getResources().getString(R.string.select_country));
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String arg0) {
etSearch.setText(arg0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Do you want to customize the SearchViewor is a standard one fine for you?
If it is I suggest looking into the ActionBar with an ActionView
Android ActionBar documentation about adding an ActionView

SearchView is occupying entire actionBar Space

In my application i am trying to add an searchView in actionbar. But while clicking on the search icon searchView is expanding to the entire actionbar while clicking the key down it is coming to normalView.
While clicking on the search icon. It is coming like this..
But I want a behavior like this..
options_menu.xml
<item
android:id="#+id/search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="ifRoom|collapseActionView"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"/>
onCreateOptions() method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
mSearchView.setBaselineAligned(false);
return super.onCreateOptionsMenu(menu);
}
please help me..Thanks in advance...
Add this line in your onCreateOptionsMenu()
searchView.setIconifiedByDefault(false);
Any particular reason to write the property twice in menu.xml ?
I just commented the code like this...Now it is working properly...
case R.id.search:
//onSearchRequested();
return true;

How to implement Voice search to SearchView

I want to add voice search function to my application. I'm populating a SearchView in SherlockActivity. But I can't find a solution to add voice search function to SearchView object.
Can you please give an advice, what do I need to do?
Code below :
public class MainActivity extends SherlockActivity {
private SlidingMenu slidingMenu;
private SlidingMenu slidingMenuRight;
private String mFilterArrays[];
public long lastScrollTime=0; /** En son kaydırma ne zaman yapıldı*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
//Create the search view
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search...");
menu.add("Search")
.setIcon(R.drawable.ic_search_inverse)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
}
Mainfest
<activity
android:name="com.paea.bcp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.paea.bcp.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
In your res/xml folder, you should have a searchable file (usually called searchable.xml).
Within the <searchable /> element in that file, you should add this attribute:
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
There are other voice-related attributes (currently voicePromptText, voiceLanguageModel and voiceLanguage) which are all described here.
You can check out the documentation here
Update: If your SearchActivity is the same one, you can override onNewIntent and handle the Search intent there. Also make your activity as singleTop, that way only one instance of the activity will stay on top.
Hope this helps and please do notify if you find a solution.
Cheers!
In case you have a SearchView and not a SearchActivity you can do these two steps to solve such problem:
Declare searchable settings in the manifest.xml inside activity's tags like this:
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
Then associate searchable settings with the SearchView in your activity class (i.e. in onCreateOptionsMenu() methods) like this:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findViewById(R.id.search);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
For more information see docs.

Categories

Resources