gpt4 book ai didi

java - 错误: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference

转载 作者:行者123 更新时间:2023-12-02 10:44:39 36 4
gpt4 key购买 nike

我使用 onTextChange Listener 中的 edit text.so 实现了搜索功能,我调用了 get 过滤器方法。我的适配器是从适配器基类扩展的,而不是从数组适配器类扩展的。因此,在我的适配器类中,我创建了过滤器方法并在 ontextChange 监听器中调用该过滤器方法。当我在搜索栏中输入文本时,我的应用程序因以下错误而崩溃:尝试在空对象引用上调用虚拟方法“void android.widget.Filter.filter(java.lang.CharSequence)”。这是我的适配器类:

public class Splitadapter extends BaseAdapter implements Filterable{
/*
* Variables Declaration section
*
*
*/
private ArrayList<COntactsModel> _Contacts;
private Context mContext;
private LayoutInflater inflater;
private ValueFilter valueFilter;
private ArrayList<COntactsModel> mStringFilterList;

public Splitadapter(Context context,ArrayList<COntactsModel> _Contacts){
super();
mContext = context;
this._Contacts = _Contacts;
mStringFilterList = _Contacts;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getFilter();

}//End of CustomAdapter constructor

public int getCount(){
return _Contacts.size();
}//End of getCount method

public Object getItem(int position){
return _Contacts.get(position).getName();
}//End of getItem method

public long getItemId(int position){
return position;
}//End of getItemId method

public class ViewHolder{
TextView textviewName;
TextView textviewNumber;
CheckBox checkbox;
ImageView image;
int id;
}

public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
final int pos = position;

if (convertView == null){
holder = new ViewHolder();

convertView = LayoutInflater.from(mContext).inflate(R.layout.list, null);
holder.textviewName = (TextView) convertView.findViewById(R.id.name);
holder.textviewNumber = (TextView) convertView.findViewById(R.id.mobile);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
holder.image = convertView.findViewById(R.id.image);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}//End of else

holder.checkbox.setId(position);
holder.textviewName.setId(position);
holder.textviewNumber.setId(position);


holder.textviewName.setText(_Contacts.get(position).getName());
holder.textviewNumber.setText(_Contacts.get(position).getPhonenum());

holder.id = position;

return convertView;
}//End of getView method

@Override
public Filter getFilter() {
if(valueFilter==null) {
valueFilter=new ValueFilter();
}
return null;
}
private class ValueFilter extends Filter {

//Invoked in a worker thread to filter the data according to the constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint!=null && constraint.length()>0){
ArrayList<COntactsModel> filterList=new ArrayList<COntactsModel>();
for(int i=0;i<mStringFilterList.size();i++){
if((mStringFilterList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
COntactsModel contacts = new COntactsModel();
contacts.setName(mStringFilterList.get(i).getName());
contacts.setPhonenum(mStringFilterList.get(i).getPhonenum());
filterList.add(contacts);
}
}
results.count=filterList.size();
results.values=filterList;
}else{
results.count=mStringFilterList.size();
results.values=mStringFilterList;
}
return results;
}


//Invoked in the UI thread to publish the filtering results in the user interface.
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
_Contacts=(ArrayList<COntactsModel>) results.values;
notifyDataSetChanged();
}
}
//End of CustomAdapter instance inner class


}

这是我的项目类:

public class COntactsModel
{
String phonenum;
String cname;
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
System.out.println("se ph num"+phonenum);
}
public String getName() {
return cname;
}
public void setName(String name) {
this.cname = name;
}
}

如何解决这个问题?

最佳答案

@Override
public Filter getFilter() {
if(valueFilter==null) {

valueFilter=new ValueFilter();

}

return null;
}

替换为

    @Override
public Filter getFilter() {
if(valueFilter==null) {

valueFilter=new ValueFilter();

}

return valueFilter;
}

关于java - 错误: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence)' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52676540/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com