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.OnQueryTextListener
interface 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();
// ...
}