这是MainActivity中的搜索视图
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
servantAdapter.getFilter().filter(text);
//Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
return true;
}
});
}
这是适配器
public class ServantAdapter extends RecyclerView.Adapter<ServantAdapter.ViewHolder>implements Filterable {
private List<Servant> mServant;
@Override
public Filter getFilter() {//The front omitted...
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
mServant = (List<Servant>) filterResults.values;
ServantAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Servant> filteredResults = null;
if (constraint.length() == 0) {
filteredResults = mServant;
} else {
filteredResults = getFilteredResults(constraint.toString().toLowerCase());
}
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
};
}
protected List<Servant> getFilteredResults(String constraint) {
List<Servant> results = new ArrayList<>();
for (Servant item : mServant) {
if (item.getName().toLowerCase().contains(constraint)) {
results.add(item);
}
}
return results;
}
为什么当我运行此程序并在searchview中输入某些内容时,程序将被关闭并返回ReportErrors java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.widget.Filter com.example.a76959.tired.ServantAdapter.getFilter()'
但是如果我在代码中使用Toast,程序可以定期运行并向我输入的消息干预(我没有在工具栏或ActionBar中使用searchview,我只将它放在MainActivity.xml中的recyclerview顶部,我不知道它是否有任何影响) 谁能帮助我...(感谢您阅读我可怜的英语Orz ..)
答案 0 :(得分:0)
当您使用<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/artist_cell_background">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@drawable/ic_music_note_24dp">
</ImageView>
<ImageButton
android:id="@+id/artist_button"
android:src="@drawable/ic_add_24dp"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/artist_cell_playlist_button_background">
</ImageButton>
<TextView
android:id="@+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/imageView"
android:layout_toEndOf="@id/imageView"
android:layout_toLeftOf="@id/artist_button"
android:layout_toStartOf="@id/artist_button">
</TextView>
<TextView
android:id="@+id/number_of_albums"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="10dp"
android:layout_toRightOf="@id/imageView"
android:layout_toEndOf="@id/imageView"
android:layout_toLeftOf="@id/artist_button"
android:layout_toStartOf="@id/artist_button"
android:layout_below="@id/artist_name">
</TextView>
</RelativeLayout>
时,请确保变量{
"error": {
"message": "(#100) An owning business is required for this request",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "C1wGrChj+O3"
}
}
已正确实例化且不为空。
使用:
servantAdapter.getFilter().filter(text);
或者:
servantAdapter