我创建了一个适配器:
public class Adapter extends BaseAdapter implements Filterable {
Context context;
ArrayList<RowBean> data = null;
private LayoutInflater inflater;
private ValueFilter valueFilter;
private ArrayList<RowBean> mStringFilterList;
public Adapter(Context context, ArrayList<RowBean> data) {
super();
this.context = context;
this.data = data;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStringFilterList = data;
getFilter();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowBeanHolder holder;
RowBean rowBean = data.get(position);
if(convertView == null){
holder = new RowBeanHolder();
convertView = inflater.inflate(R.layout.all_object_holder,null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
}else{
holder = (RowBeanHolder) convertView.getTag();
}
if(rowBean != null) {
holder.txtTitle.setText(rowBean.getTitle());
holder.selected.setChecked(rowBean.isSelected());
}
return convertView;
}
@Override
public Filter getFilter() {
if(valueFilter==null) {
valueFilter=new ValueFilter();
}
return valueFilter;
}
private class RowBeanHolder {
TextView txtTitle;
CheckBox selected;
}
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<RowBean> filterList=new ArrayList<>();
for(int i=0;i<mStringFilterList.size();i++){
if((mStringFilterList.get(i).getTitle().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
RowBean contacts = new RowBean();
contacts.setTitle(mStringFilterList.get(i).getTitle());
contacts.setSelected(mStringFilterList.get(i).isSelected());
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) {
data=(ArrayList<RowBean>) results.values;
notifyDataSetChanged();
}
}
}
当我使用此适配器启动活动时,我会看到以下内容: 过程:com.maps,PID:20174 java.lang.NullPointerException:尝试从空对象引用上的字段'android.widget.TextView com.maps.Adapter.Adapter $ RowBeanHolder.txtTitle'中读取
这是一行:
holder.txtTitle.setText(rowBean.getTitle());
这是一个布局all_object_holder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkBox12"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="false"
android:button="@xml/setting_checkbox"
android:focusable="false"
android:gravity="center" />
<TextView
android:id="@+id/showText"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:text="@string/show_choose_point"
android:textColor="#000000"
android:layout_alignBaseline="@+id/checkBox12"
android:layout_alignBottom="@+id/checkBox12"
android:layout_toRightOf="@+id/checkBox12"
android:layout_toEndOf="@+id/checkBox12" />
</RelativeLayout>
答案 0 :(得分:2)
添加行
convertView.setTag(holder);
没有它convertView.getTag()将返回null。
答案 1 :(得分:1)
if(convertView == null){
holder = new RowBeanHolder();
convertView = inflater.inflate(R.layout.all_object_holder,null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
holder.selected = (CheckBox)
convertView.setTag(holder);
}
您没有添加 convertView.setTag(holder)。请将其添加到您的代码中并进行检查。
答案 2 :(得分:0)
NullPointerException
因为您的holder
对象有时为空。您应该setTag
convertView
convertView.setTag(holder);
,只需将getView
方法添加到@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowBeanHolder holder;
RowBean rowBean = data.get(position);
if(convertView == null){
holder = new RowBeanHolder();
convertView = inflater.inflate(R.layout.all_object_holder,null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
convertView.setTag(holder);
}else{
holder = (RowBeanHolder) convertView.getTag();
}
if(rowBean != null) {
holder.txtTitle.setText(rowBean.getTitle());
holder.selected.setChecked(rowBean.isSelected());
}
return convertView;
}
方法中:
combine
祝你好运。
答案 3 :(得分:-3)
使用此代码更新您的代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowBeanHolder holder = new RowBeanHolder();
RowBean rowBean = data.get(position);
if(convertView == null){
convertView = inflater.inflate(R.layout.all_object_holder,null);
}
holder.txtTitle = (TextView) convertView.findViewById(R.id.showText);
holder.selected = (CheckBox) convertView.findViewById(R.id.checkBox12);
if(rowBean != null) {
holder.txtTitle.setText(rowBean.getTitle());
holder.selected.setChecked(rowBean.isSelected());
}
return convertView;
}