Notifydatasetinvalidated导致空指针异常

时间:2014-05-12 16:14:08

标签: android baseadapter

我在我的适配器类中过滤数据时遇到空指针异常。这是代码。搜索工作正常但在完成搜索时调用notifyDataSetInvalidated时会发生错误。

适配器类:

public class SongAdapter extends BaseAdapter implements Filterable {
private ArrayList<Song> songs=new ArrayList<Song>();
private LayoutInflater songInf;
private ArrayList<Song> glossariesListForSearch;
private Context mContext;
int layoutResourceId;

public SongAdapter(Context c, ArrayList<Song> songs) {
    super();
    this.mContext = c;
    this.songs = songs;
    this.glossariesListForSearch = songs;
    this.songInf = LayoutInflater.from(c);
}

@Override
public int getCount() {

    return songs.size(); // error here
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int position) {

    return position;
}

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

    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = (LinearLayout) inflater
                .inflate(R.layout.song, parent, false);
    }
    final int pos = position;

    TextView textView = (TextView) view.findViewById(R.id.tv_song_title);
    textView.setText(songs.get(position).getTitle());

    textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.d(this.getClass().getName(), "onClick");
            ((MainActivity) mContext).songPicked(pos);
        }
    });

    view.setTag(position);
    return view;

}

@Override
public Filter getFilter() {

    return myFilter;
}

Filter myFilter = new Filter() {

    @SuppressWarnings("unchecked")
    @Override
    public void publishResults(CharSequence constraint,
            FilterResults results) {
        songs = (ArrayList<Song>) results.values;

        if (results.count > 0) {
            Log.i("Filtering", "Results count>0");
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated(); //error here
        }

    }

    @Override
    public FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        ArrayList<Song> tempGlossaryList = new ArrayList<Song>();

        if (constraint != null && constraint.toString().length() > 0) {
            int length = glossariesListForSearch.size();
            Log.i("Filtering", "glossaries size" + length);
            int i = 0;
            while (i < length) {

                Song item = glossariesListForSearch.get(i);
                // Real filtering:
                if (item.getTitle().toLowerCase()
                        .contains(constraint.toString().toLowerCase())) {
                    tempGlossaryList.add(item);
                }
                i++;
            }

            filterResults.values = tempGlossaryList;
            filterResults.count = tempGlossaryList.size();
            Log.i("Filtering", "Filter result count size"
                    + filterResults.count);
        }
        return filterResults;
    }
};

    }

05-12 12:12:13.311:E / AndroidRuntime(1809):java.lang.NullPointerException 05-12 12:12:13.311:E / AndroidRuntime(1809):at com.siju.multimediaplayer.SongAdapter.getCount(SongAdapter.java:53) 05-12 12:12:13.311:E / AndroidRuntime(1809):在android.widget.AdapterView.checkFocus(AdapterView.java:712) 05-12 12:12:13.311:E / AndroidRuntime(1809):在android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated(AdapterView.java:835) 05-12 12:12:13.311:E / AndroidRuntime(1809):在android.widget.AbsListView $ AdapterDataSetObserver.onInvalidated(AbsListView.java:6006) 05-12 12:12:13.311:E / AndroidRuntime(1809):在android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50) 05-12 12:12:13.311:E / AndroidRuntime(1809):在android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59) 05-12 12:12:13.311:E / AndroidRuntime(1809):at com.siju.multimediaplayer.SongAdapter $ 1.publishResults(SongAdapter.java:116)

2 个答案:

答案 0 :(得分:0)

请检查结果的空状态

if (results!=null) {
   if (results.count > 0) {
        Log.i("Filtering", "Results count>0");
        notifyDataSetChanged();
    } else {
        notifyDataSetInvalidated(); //error here
    }
}

答案 1 :(得分:0)

通过更改单行来解决:

    if (constraint != null && constraint.toString().length() > 0)

更改为

   if (constraint != null && glossariesListForSearch!=null) 

现在有效:)