我正在为listview创建一个过滤器,我从json获取数据。 这是我的代码 SearchView:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
return true;
}@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
adapter.getFilter().filter("");
lv.clearTextFilter();
}else {
adapter.getFilter().filter(newText.toString());
}
return true;
}
点击搜索按钮后,这是我的logcat
FATAL EXCEPTION: main
Process: com.example.trung.flashcardenglish, PID: 5400
java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.ArrayAdapter.getFilter()' on a null object reference
at com.example.trung.flashcardenglish.ielts.Ielts.onQueryTextChange(Ielts.java:244)
at android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1150)
at android.support.v7.widget.SearchView.access$2000(SearchView.java:103)
at android.support.v7.widget.SearchView$12.onTextChanged(SearchView.java:1680)
at android.widget.TextView.sendOnTextChanged(TextView.java:7679)
at android.widget.TextView.setText(TextView.java:4060)
这是我的适配器:
lv.setAdapter(适配器); lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
String vocabulary = ((TextView) view.findViewById(R.id.vocabulary)).getText()
.toString();
Intent in = new Intent(getApplicationContext(),
ielts_detail.class);
in.putExtra(TAG_PID, pid);
in.putExtra(TAG_VOCABULARY, vocabulary);
startActivity(in);
startActivityForResult(in, 100);
}
});
}