在适配器中我有一个过滤器,当我尝试找到我在列表中没有的东西时,我看到了:
07-31 16:17:07.025: E/AndroidRuntime(4634): java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
07-31 16:17:07.025: E/AndroidRuntime(4634): at pl.eltegps.inwentaryzja.offline.Adapter.getView(Adapter.java:105)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.widget.AbsListView.obtainView(AbsListView.java:2467)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.widget.ListView.makeAndAddView(ListView.java:1894)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.widget.ListView.fillSpecific(ListView.java:1360)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.widget.ListView.layoutChildren(ListView.java:1673)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.widget.AbsListView.onLayout(AbsListView.java:2230)
07-31 16:17:07.025: E/AndroidRuntime(4634): at android.view.View.layout(View.java:16001)
这是我的适配器:
public class Adapter extends BaseAdapter implements Filterable {
public Context context;
public static ArrayList<AdapterRooteEntity> data;
private LayoutInflater inflater;
public static ArrayList<AdapterRooteEntity> mStringFilterList;
private ValueFilter valueFilter;
GPSTracker2 gps;
int inx;
public Adapter(Context context, ArrayList<AdapterRooteEntity> data) {
this.context = context;
this.data = data;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStringFilterList = data;
getFilter();
this.gps = new GPSTracker2(context);
inx = 1;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return data.indexOf(data.get(position)) + 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AdapterRooteEntity row = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.all_object_holder, null);
}
DecimalFormat form = new DecimalFormat("0.00");
RowBeanHolder holder = new RowBeanHolder();
holder.tvAddress = (TextView) convertView.findViewById(R.id.tv_address);
holder.tvOwner = (TextView) convertView.findViewById(R.id.tv_owner);
holder.tvId = (TextView) convertView.findViewById(R.id.tv_id);
holder.tvDistance = (TextView) convertView.findViewById(R.id.tv_distance);
holder.tvId.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
if (row != null) {
holder.tvAddress.setText(row.getAddress());
holder.tvOwner.setText(row.getOwner());
holder.tvId.setText(getItemId(position) + "");
inx++;
if (row.getLat() > 0.0 && row.getLon() > 0.0) {
if (gps.canGetLocation) {
double dis = Tools.distance(gps.getLatitude(), row.getLat(), gps.getLongitude(), gps.getLongitude());
data.get(position).setDistance(dis);
holder.tvDistance.setText(form.format(dis) + " km");
}
} else {
holder.tvDistance.setText("?.? km");
}
if (RootActivity.chooseRow != null) {
if (RootActivity.chooseRow.getAddress().equals(row.getAddress()) && RootActivity.chooseRow.getId() == row.getId()) {
convertView.setBackgroundColor(context.getResources().getColor(R.color.listSelectedItem));
holder.tvAddress.setTextColor(Color.WHITE);
holder.tvOwner.setTextColor(Color.WHITE);
holder.tvId.setTextColor(Color.WHITE);
holder.tvDistance.setTextColor(Color.WHITE);
} else {
convertView.setBackgroundColor(Color.WHITE);
holder.tvAddress.setTextColor(Color.BLACK);
holder.tvOwner.setTextColor(Color.BLACK);
holder.tvId.setTextColor(Color.BLACK);
holder.tvDistance.setTextColor(Color.BLACK);
}
}
// for(BIN bin : row.getBins()){
if(!row.getBins().isEmpty()){
for(int a= 0 ; a < row.getBins().size(); a++){
BIN bin = row.getBins().get(a);
if(StringUtils.isEmpty(bin.getBarcode()) && StringUtils.isEmpty(bin.getRfid1()) ){
row.setDone(false);
}
}
}
if(row.isDone()){
holder.tvId.setBackgroundColor(context.getResources().getColor(R.color.green));
}else{
holder.tvId.setBackgroundColor(context.getResources().getColor(R.color.red));
}
}
return convertView;
}
private class RowBeanHolder {
TextView tvAddress;
TextView tvOwner;
TextView tvId;
TextView tvDistance;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
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<AdapterRooteEntity> filterList = new ArrayList<AdapterRooteEntity>();
int ii = 1;
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getAddress().toUpperCase()).contains(constraint.toString().toUpperCase())) {
AdapterRooteEntity row = new AdapterRooteEntity();
row.setAddress(mStringFilterList.get(i).getAddress());
row.setOwner(mStringFilterList.get(i).getOwner());
row.setId(ii);
filterList.add(row);
i++;
}
}
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<AdapterRooteEntity>) results.values;
notifyDataSetChanged();
}
}
}
这里我的应用程序崩溃了。当我写一个我没有列在我的列表上的字符时,就会发生这种情况,当我评论这一行时,当我使用过滤器并尝试查找我的列表中没有的内容时,我会看到空列表而我的apk不是轻率的
if(!row.getBins().isEmpty()){
for(int a= 0 ; a < row.getBins().size(); a++){
BIN bin = row.getBins().get(a);
if(StringUtils.isEmpty(bin.getBarcode()) && StringUtils.isEmpty(bin.getRfid1()) ){
row.setDone(false);
}
}
}