Home How to use same searchView for 2 different methods?
Reply: 0

How to use same searchView for 2 different methods?

user1172 Published in July 17, 2018, 11:11 pm

in one method, onCreateOptionsMenu(final Menu menu) I created a searchView but now how do I carry this same searchView onto my onOptionsItemSelected(MenuItem item) method?

public boolean onCreateOptionsMenu(final Menu menu) {

    final MenuItem item =menu.findItem(menuSearch);
    final  SearchView searchView = (SearchView) item.getActionView();


public boolean onOptionsItemSelected(MenuItem item) {
  SearchView searchView = (SearchView) item.getActionView();  //<--crashes app 
    searchView.onActionViewCollapsed(); //<--"Cannot resolve symbol 'searchView'"

 return super.onOptionsItemSelected(item);

In onOptionsItemSelected(MenuItem item) the word "searchView" turns red and I get error stating "Cannot resolve symbol 'searchView'". When I click on the "back" actionBar, now the app crashes.

How do I carry over this same searchView from onCreateOptionsMenu(final Menu menu) to onOptionsItemSelected(MenuItem item) ?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300213 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO