Why searchview is a null object reference? - android

I haven't change any code last week, but this problem suddenly appeared yesterday.
Here is method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
});
return super.onCreateOptionsMenu(menu);
}
When I try to open activity that contains SearchView, app closes. Logcat says: attempt to invoke virtual method searchView.setOnQueryTextListener() on a null object reference.
search_menu:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#mipmap/ic_search"
android:title="#string/search"
android:inputType="text"
android:focusableInTouchMode="false"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
What is wrong with searchview? I've tried all methods offered in similar questions, but they do not work.
Activity:
public class CategoryActivity extends Activity {
ArrayList<> mArrayList;
CustomAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mArrayList = new ArrayList<>();
Objects.requireNonNull(getActionBar()).setBackgroundDrawable(new ColorDrawable(Color.rgb(8,16,38)));
Objects.requireNonNull(getActionBar()).setDisplayHomeAsUpEnabled(true);

You declare in menu xml the wrong type of SearchView:
Change
app:actionViewClass="android.widget.SearchView"
to
app:actionViewClass="android.support.v7.widget.SearchView"
also change:
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
to:
SearchView searchView = (SearchView) item.getActionView();
the signature of getActionView() you use is deprecated.
Edit:
In your activity's class, check the imports on top, there must be a line:
import android.support.v7.widget.SearchView;

Related

Getting SearchView from MenuItem returns View and not SearchView

I'm trying to fetch my SearchView from the Toolbar within a Fragment.
Im infalting a menu item in the onCreateOptionsMenu.
Problem : searchItem.getActionView() returns a "View" and not a "SearchView". See code below
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = searchItem.getActionView(); // This one is red - "returns View, should return Searchview"
}
XML for menu item (menu_search.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/search"
android:id="#+id/search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Try this :
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();

(SearchView) MenuItemCompat.getActionView(item) always null

i am beginner in android studio and sorry my English is not good.
i have a searchView in secondActivity.
i have done things like below :
1.implemented the v7 AppCompat support library in gradle like this compile 'com.android.support:appcompat-v7:25.3.1'
2.implemented android.support.v7.widget.SearchView.OnQueryTextListenerinterface for current activity like this public class ViewActivity extends AppCompatActivity implements SearchView.OnQueryTextListener
but the searchView is still null.
below are my source code:
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_search1"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android:support.v7.widget.SearchView"
android:title="#string/search_hint" />
</menu>
in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu){
getMenuInflater().inflate(R.menu.menu_search, menu);
final MenuItem item = menu.findItem(R.id.action_search1);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setQueryHint("Search Name");
searchView.setIconified(false);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
MenuItemCompat.getActionView(MenuItem item) is deprecated in API level 26.1.0.
Now the recommended way is calling getActionView() directly on the menu item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
// ...
}

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

How to collapse SearchView on backpressed using appcompat v7.?

I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.
menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_action_sort"
android:title="#string/sort"
app:showAsAction="ifRoom"/>
</menu>
java file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?
Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.yourapp.youractivity">
<item
android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/nav_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
This is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}
In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).
Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()

Remove icon/title for actionbar with expanded SearchView?

I created a new project, targeting api 11 and above. I have an activity where I want a SearchView expanded by default:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setIconifiedByDefault(false);
}
which works, but I don't want any icon or title on the actionbar, just the searchview. I tried getting rid of my title and icon by doing:
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(android.R.color.transparent);
But my actionbar still has this padding to the left of the searchview:
Is there a way to get rid of it?
Thanks
-------- Edit --------------------
Here's my activity in full:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
return true;
}
}
and here's the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Even though the icon is transparent, it still takes up the space:
getActionBar().setIcon(android.R.color.transparent);
Remove this line and add:
getActionBar().setDisplayShowHomeEnabled(false);
Now, you should have the SearchView take up all the available space.
Action items that can be collapsed have a max width. The only workaround I can think of is the following:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
// You can use Display.getSize(Point) from API 13 onwards.
// For API 11 and 12, use Display.getWidth()
final Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);
// Create LayoutParams with width set to screen's width
LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);
mSearchView.setLayoutParams(params);
return true;
}
}
Although it looks like there's still space to the left, you can use mSearchView.setBackgroundColor(Color.RED); to see that there really isn't.
Regarding left padding you could try using collapseActionView
Here is an example menu:
<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="#android:drawable/ic_menu_search"
android:title="#string/search_hint"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
I could not get #Vikram's awnser to work, but I found that another answer from Stack Overflow:
Open your styles.xml file and add below codes in your Actionbar style
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
<item name="displayOptions">showHome|homeAsUp|showTitle</item>
<item name="android:icon">#android:color/transparent</item> <--this do the magic!
p/s: I'm using Actionbar Sherlock and this works just fine
(See Remove icon/logo from action bar on android work)

Categories

Resources