Android 使用筛选器时notifyDataSetChanged()上的应用程序崩溃

Android 使用筛选器时notifyDataSetChanged()上的应用程序崩溃,android,listview,android-listview,android-spinner,Android,Listview,Android Listview,Android Spinner,我有一个包含微调器、图像和文本视图的ListView。在这个ListView上面,我有一个EditText,用户可以在其中键入所需的项名称,并且所有相关项都已显示在ListView中。我的代码一直工作到这里,但问题是当我试图删除应用程序刚刚崩溃的最后一个字符时。我认为listview没有正确刷新。我已经搜索了很多答案,但没有找到与我的问题相关的任何东西,所以请在应用程序carsh问题中帮助我 下面是我在LazyAdapter中的getFilter代码,它是从BaseAdapter扩展而来的 @S

我有一个包含微调器、图像和文本视图的ListView。在这个ListView上面,我有一个EditText,用户可以在其中键入所需的项名称,并且所有相关项都已显示在ListView中。我的代码一直工作到这里,但问题是当我试图删除应用程序刚刚崩溃的最后一个字符时。我认为listview没有正确刷新。我已经搜索了很多答案,但没有找到与我的问题相关的任何东西,所以请在应用程序carsh问题中帮助我

下面是我在LazyAdapter中的getFilter代码,它是从BaseAdapter扩展而来的

@SuppressLint("DefaultLocale")
    public android.widget.Filter getFilter() {

        return new android.widget.Filter() {

            @SuppressLint("DefaultLocale")
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults Result = new FilterResults();
                // if constraint is empty return the original names
                if (TextUtils.isEmpty(constraint)){
                    //Result.values = storedata;
                    Result.count = listDetails.size();
                    return Result;
                }

                ArrayList<items> Filtered_Names = new ArrayList<items>();
                String filterString = constraint.toString().toLowerCase();
                String filterableString;

                for(int i = 0; i<listDetails.size(); i++){
                     items searchdata = listDetails.get(i);
                    String title = searchdata.title();
                    String id = searchdata.id();
                    String imageUrl = searchdata.imageUrl();
                    String pprice = searchdata.pprice();
                    String shippingPrice = searchdata.shippingPrice();
                    String stock = searchdata.stock();

                    filterableString = title+" "+id+" "+imageUrl+" "+pprice+" "+shippingPrice+" "+status+" "+stitle+" "+stock+" 
                    if(filterableString.toLowerCase().contains(filterString)){
                    Filtered_Names.add(listDetails.get(i));
                    //Log.e("Added", String.valueOf(Filtered_Names.size()));
                    }

                    }
                Result.values = Filtered_Names;
                Result.count = Filtered_Names.size();
                //Log.e("Results", Result.values.toString() + String.valueOf(Result.count));
                return Result;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                // TODO Auto-generated method stub
                 @SuppressWarnings("unchecked")
                ArrayList<items> resultList =(ArrayList<items>) results.values;
    LazyAdapter.this.listDetails = resultList;
    LazyAdapter.this.notifyDataSetChanged();
            }

        };

    }
这是logcat说的

**

**

这是我的getCount()方法


您可能在某个地方初始化了ArrayList,然后第一次调用getCount()返回正确的结果。然后发布新结果,并将数组设置为null,因为不进行检查

这会导致getCount()抛出NullPointerException。 简单的解决方法是将getCount()方法更改为如下内容:

public int getCount() {
   return listDetails == null ? 0 : listDetails.size();
}
更好的方法是在
publishResults
方法中执行空检查,并相应地处理适配器字段的初始化

Edit:从上面的注释可以看出,构造函数接受ArrayList作为参数。因此,在代码中的某个地方,您必须使用以下内容初始化LazyAdapter:

newlazyadapter(上下文,newarraylist())


这会初始化listDetails ArrayList,而您一开始不会得到NPE。

您可能会在某个地方初始化ArrayList,然后第一次调用getCount()返回正确的结果。然后发布新结果,并将数组设置为null,因为不进行检查

这会导致getCount()抛出NullPointerException。 简单的解决方法是将getCount()方法更改为如下内容:

public int getCount() {
   return listDetails == null ? 0 : listDetails.size();
}
更好的方法是在
publishResults
方法中执行空检查,并相应地处理适配器字段的初始化

Edit:从上面的注释可以看出,构造函数接受ArrayList作为参数。因此,在代码中的某个地方,您必须使用以下内容初始化LazyAdapter:

newlazyadapter(上下文,newarraylist())



这将初始化listDetails ArrayList,您一开始不会得到NPE。

listDetails在调用getCount()方法时显然为null。可能会更改为
返回listDetails==null?0:listDetails.size()
@peshkira你是说在getFilter调用listDetails之后,listDetails变为null?虽然一开始视图创建成功。我不能确定,但我假设在您的
publishResults
中,设置为LazyAdapter的ArrayList为空。也许在那里添加一个检查来验证这一点。是的,你是对的,但我想知道为什么getCount()在视图成功创建时最初不返回null,并且在getFilter()调用之后返回null。我再次猜测-在你的LazyAdapter中,构造函数中的某个地方有这样的东西-
ArrayList listDetails=new ArrayList()listDetails显然为null。可能会更改为
返回listDetails==null?0:listDetails.size()
@peshkira你是说在getFilter调用listDetails之后,listDetails变为null?虽然一开始视图创建成功。我不能确定,但我假设在您的
publishResults
中,设置为LazyAdapter的ArrayList为空。也许在那里添加一个检查来验证这一点。是的,你是对的,但我想知道为什么getCount()在视图成功创建时最初不返回null,并且在getFilter()调用之后返回null。我再次猜测-在你的LazyAdapter中,构造函数中的某个地方有这样的东西-
ArrayList listDetails=new ArrayList()在你说我做了什么之后,应用程序没有接近,但在搜索框中输入文本后列表视图为空。logcat现在在com.example.itemspac.LazyAdapter.(LazyAdapter.java:36)的com.example.itemspac.LazyAdapter.(LazyAdapter.java:41)
上显示
com.example.itemspac.LazyAdapter.(LazyAdapter.java:36),第36行是构造函数的第一行。这是第36行
公共LazyAdapter(上下文上下文,数组列表)
和41是图像下载器这似乎是正确的行为。应用程序不会崩溃,并且您的列表视图为空,因为您的筛选器返回空(null)数组列表。我相信这回答了这个问题,如果你还有其他问题,你应该单独问。是的,伙计,这是答案,但是告诉我应该在哪里初始化这行new
LazyAdapter(context,new ArrayList())
,以避免空过滤器?回答我第二个问题,我将接受这两个问题。这是在你说我做了什么之后,应用程序没有接近,但在搜索框中输入文本后列表视图为空。logcat现在在com.example.itemspac.LazyAdapter.(LazyAdapter.java:36)的com.example.itemspac.LazyAdapter.(LazyAdapter.java:41)
上显示
com.example.itemspac.LazyAdapter.(LazyAdapter.java:36),第36行是构造函数的第一行。这是第36行
公共LazyAdapter(上下文上下文,数组列表)
和41是图像下载器这似乎是正确的行为。应用程序不会崩溃,并且您的列表视图为空,因为您的筛选器返回空(null)数组列表。我相信这回答了这个问题,如果你还有其他问题,你应该单独问。是的,伙计,这是答案,但是告诉我应该在哪里初始化这行new
LazyAdapter(context,new ArrayList())
,以避免空过滤器?回答我第二个问题,我将接受这两个问题。这是
@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listDetails.size();
    }
public int getCount() {
   return listDetails == null ? 0 : listDetails.size();
}