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

How to use same searchView for 2 different methods?

iBEK
1#
iBEK Published in 2017-11-15 00:41:38Z

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?

@Override
public boolean onCreateOptionsMenu(final Menu menu) {

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

     searchView.onActionViewCollapsed();
 }

@Override
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.295553 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO