簡體   English   中英

活動開始時的Java android適配器Nullpointer

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

當我使用此適配器開始活動時,我記錄了以下內容:進程: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()將返回null。

 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) 請將其添加到您的代碼中並檢查。

發生NullPointerException原因是您的holder對象有時為空。 您應該設置convertView setTag ,只需添加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;
}

祝好運。

使用此代碼更新您的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM