Java android Activity 启动时适配器空指针

Java android Adapter Nullpointer when Activity start

我创建了一个适配器:

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();
        }
    }
}

当我使用此适配器启动我的 activity 时,我在日志中看到: 进程: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>

添加行

convertView.setTag(holder);

没有它 convertView.getTag() 将 return 为空。

使用此代码更新您的代码

@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;
}

NullPointerException 发生是因为您的 holder 对象有时为空。您应该 convertViewsetTag,只需将 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;
}

祝你好运。

 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(持有人)。请将其添加到您的代码中并检查。