我得到一個空指針異常,同時過濾我的適配器類中的數據。這裏是代碼。搜索工作正常,但在完成搜索時調用notifyDataSetInvalidated時發生錯誤。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;
}
};
}
12月5日至12日:12:13.311:E/AndroidRuntime(1809):顯示java.lang.NullPointerException 12月5日至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):at android.widget.AdapterView.checkFocus(AdapterView.java:712 ) 05-12 12:12:13.311:E/AndroidRuntime(1809):at android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated(AdapterView.java:835) 05-12 12:12:13.311:E/AndroidRuntime(1809) ):在android.widget.AbsListView $ AdapterDa taSetObserver.onInvalidated(AbsListView.java:6006) 05-12 12:12:13.311:E/AndroidRuntime(1809):at 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)
您的解決方案是行不通的。 – Siju