我在android中创建了一个searchview。在OnQueryTextListener中使用OnQueryTextChange()并将getFilter()添加到适配器并运行应用程序时,应用程序正确打开,但在搜索栏中键入文本时会关闭应用程序并在onQueryTextChange()附近显示java.lang.NullpointException错误。 请帮助解决我的问题。下面是代码。
MainActivity
公共类MainActivity扩展了AppCompatActivity {
SearchView searchView;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar toolbar= new Toolbar(this);
// setSupportActionBar(toolbar);
searchView= (SearchView)findViewById(R.id.search);
recyclerView=(RecyclerView)findViewById(R.id.recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
final MyAdapter myadapter= new MyAdapter(this,getStudents());
recyclerView.setAdapter(myadapter);
if(searchView!=null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
myadapter.getFilter().filter(query);
return false;
}
});
}
}
private ArrayList<Students> getStudents(){
ArrayList<Students> students=new ArrayList<>();
Students s= new Students();
s.setImg(R.drawable.facebook);
s.setName("Girish");
s.setPosition("First");
students.add(s);
s=new Students();
s.setImg(R.drawable.instagram);
s.setName("Sirish");
s.setPosition("second");
students.add(s);
s=new Students();
s.setImg(R.drawable.twitter);
s.setName("Harish");
s.setPosition("Third");
students.add(s);
s=new Students();
s.setImg(R.drawable.youtube);
s.setName("Manish");
s.setPosition("fourth");
students.add(s);
s=new Students();
s.setImg(R.drawable.like);
s.setName("Mahesh");
s.setPosition("fifth");
students.add(s);
s=new Students();
s.setImg(R.drawable.googleplus);
s.setName("Raj");
s.setPosition("Sixth");
students.add(s);
s=new Students();
s.setImg(R.drawable.snapchat);
s.setName("Ramesh");
s.setPosition("Seventh");
students.add(s);
return students;
}
}
MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyHolder> implements Filterable{
Context c;
ArrayList<Students> students, filterlist;
CustomFilter filter;
public MyAdapter(Context context, ArrayList<Students> students) {
this.c=context;
this.students=students;
this.filterlist=students;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder myHolder=new MyHolder(v);
return myHolder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.positionTxt.setText(students.get(position).getPosition());
holder.nameTxt.setText(students.get(position).getName());
holder.img.setImageResource(students.get(position).getImg());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Toast.makeText(c, "item created", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return students.size();
}
@Override
public Filter getFilter() {
if(filter==null){
filter= new CustomFilter(filterlist,this);
}
return null;
}
}