数组适配器中的空指针异常

时间:2016-02-01 12:39:47

标签: android nullpointerexception android-arrayadapter

为我的自动填充文本视图创建自定义数组适配器

  

java.lang.NullPointerException:尝试调用虚方法' int java.util.ArrayList.size()'在一个空对象上

公共类SkillsAutocompleteAdapter扩展了ArrayAdapter {

Context mContext;
int layoutResourceId;
private ItemFilter mFilter = new ItemFilter();
private ArrayList<Skills> originalData;
private ArrayList<Skills> filteredData;

public  SkillsAutocompleteAdapter(Context mContext, int layoutResourceId, ArrayList<Skills> list) {
    super(mContext, layoutResourceId, list);

    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    filteredData = list;
    originalData = list;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    if (rowView == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        rowView = inflater.inflate(layoutResourceId, parent, false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.rdoText = (TextView) rowView.findViewById(R.id.txtTitle);
        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    Skills folder = filteredData.get(position);
    holder.rdoText.setText(folder.getSkill());
    holder.rdoText.setTag(folder.getSkillId());

    return rowView;
}

@Override
public int getCount() {
    return filteredData.size();
}

@Override
public Skills getItem(int position) {
    return filteredData.get(position);
}

@Override
public long getItemId(int position) {
    return filteredData.get(position).getSkillId();
}

public Filter getFilter() {
    return mFilter;
}

static class ViewHolder {
    public TextView rdoText;
}

private class ItemFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();
        FilterResults results = new FilterResults();
        int count = originalData.size();
        final ArrayList<Skills> tempFilterList = new ArrayList<Skills>(count);
        String filterableString;

        for (int i = 0; i < count; i++) {
            filterableString = originalData.get(i).getSkill();
            if (filterableString.toLowerCase().contains(filterString)) {
                tempFilterList.add(originalData.get(i));
            }
        }

        results.values = tempFilterList;
        Log.i("test", "3");
        results.count = tempFilterList.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<Skills>) results.values;
        notifyDataSetChanged();
    }
}

 }

活动代码

 autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.editAutoComplete);

    skillsArrayList = new ArrayList<Skills>();
    skillsArrayList.add(new Skills(1, "Software Developer"));
    skillsArrayList.add(new Skills(2, "Tester"));
    skillsArrayList.add(new Skills(3, "Linux"));
    skillsArrayList.add(new Skills(4, "Java"));
    skillsArrayList.add(new Skills(5, "Dot Net"));
    skillsArrayList.add(new Skills(6, "PHP"));
    skillsArrayList.add(new Skills(7, "Node Js"));

    if (skillsArrayList != null) {
        skillsAutoAdapter = new SkillsAutocompleteAdapter(this, R.layout.row_autocomplettextview, skillsArrayList);
        autoCompleteTextView.setAdapter(skillsAutoAdapter);
    }

这是mylog

FATAL EXCEPTION: main
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime: Process: com.okayconnect.app, PID: 13543
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at com.okayconnect.app.adapters.SkillsAutocompleteAdapter.getCount(SkillsAutocompleteAdapter.java:60)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1297)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:155)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5696)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-01 18:07:49.115 13543-13543/com.okayconnect.app E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

**Find my answer i add following line to my code **

@Override
public int getCount() {
    Log.i("test", "size");
    if (filteredData != null)
        return filteredData.size();
    else
        return 0;
}

2 个答案:

答案 0 :(得分:0)

Use following:-

this.filteredData.addAll(list);
this.originalData.addAll(list);

答案 1 :(得分:0)

更改以下行 -

private ArrayList<Skills> originalData = new ArrayList<Skills>();
private ArrayList<Skills> filteredData = new ArrayList<Skills>();

SkillsAutocompleteAdapter(Context mContext, int layoutResourceId, ArrayList<Skills> list) {
    super(mContext, layoutResourceId, list);

    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.filteredData = list;
    this.originalData = list;
}

到 -

private ArrayList<Skills> originalData;
private ArrayList<Skills> filteredData;
int count = 0;

SkillsAutocompleteAdapter(Context mContext, int layoutResourceId, ArrayList<Skills> list) {
    super(mContext, layoutResourceId, list);

    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    filteredData = list;
    originalData = list;
    count = list.size()
}

@Override
public int getCount() {
    //return filteredData.size();
return count;
}