我正在恢复以前开发的旧应用程序,并且解决了一些问题。 我目前面临的是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
当然,这是因为对getActionView()的previos调用返回null。 我试图更新依赖项,但没有任何效果。
这是我尝试运行的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
et = (SearchView) searchItem.getActionView();
et.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
}
}
});
et.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
expListAdapter.getFilter().filter(newText);
cmAdapter.getFilter().filter(newText);
allAdapter.getFilter().filter(newText);
playlistAdapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
});
return true;
}
<?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:title="Shuffle All"
android:icon="@drawable/ic_action_shuffle"
android:id="@+id/shuffle"
android:orderInCategory="2"
app:showAsAction="always" />
<item android:id="@+id/search"
android:title="@string/search_hint"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>