应用程序在使用过滤器的 notifyDataSetChanged() 上崩溃

Abi*_*han 0 android listview android-listview android-spinner

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

这是我在从 BaseAdapter 扩展的 LazyAdapter 中的 getFilter 代码

@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();
            }

        };

    }
Run Code Online (Sandbox Code Playgroud)

这是活动中的代码

inputSearch = (EditText) findViewById(R.id.inputsearch);
if(adapter!=null){
}
 inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            try{
                ItemsActivity.this.adapter.getFilter().filter(cs.toString());  
            }catch(Exception e){
                e.printStackTrace();
            }

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub 

        }
    }); 
Run Code Online (Sandbox Code Playgroud)

这是 logcat 所说的

**

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.itemspac.LazyAdapter.getCount(LazyAdapter.java:48)
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at com.example.itemspac.LazyAdapter$3.publishResults(LazyAdapterGifts.java:305)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
Run Code Online (Sandbox Code Playgroud)

**

这是我的 getCount() 方法

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listDetails.size();
    }
Run Code Online (Sandbox Code Playgroud)

pes*_*ira 5

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

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

public int getCount() {
   return listDetails == null ? 0 : listDetails.size();
}
Run Code Online (Sandbox Code Playgroud)

更好的是在publishResults方法中进行空检查并相应地处理适配器字段的初始化。

编辑:从上面的评论中你说你的构造函数接受一个 ArrayList 作为参数。因此,您必须在代码中的某处使用以下内容初始化 LazyAdapter:

new LazyAdapter(context, new ArrayList())

这会初始化 listDetails ArrayList 并且您一开始不会得到 NPE。