Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getItemCount方法中存在Null指针异常_Java_Android_Filter_Android Recyclerview - Fatal编程技术网

Java getItemCount方法中存在Null指针异常

Java getItemCount方法中存在Null指针异常,java,android,filter,android-recyclerview,Java,Android,Filter,Android Recyclerview,我正在使用带有卡片视图和recyclerview的列表。我为列表添加了适配器,并添加了getItemCount方法。我想为我的列表添加一个搜索功能。所以我也添加了一个过滤器。但是当我在搜索视图中键入时,我在returnitemlist.size()上得到一个异常;这条线。。我错过了什么 适配器 public class ItemAdapter extends RecyclerView.Adapter<ListViewHolder> implements Filterable{

我正在使用带有卡片视图和recyclerview的列表。我为列表添加了适配器,并添加了getItemCount方法。我想为我的列表添加一个搜索功能。所以我也添加了一个过滤器。但是当我在搜索视图中键入时,我在returnitemlist.size()上得到一个异常;这条线。。我错过了什么

适配器

public class ItemAdapter  extends RecyclerView.Adapter<ListViewHolder> implements Filterable{

    public ItemsFilter itemFilter;
    ArrayList<Item> mStringFilterList;
    private ArrayList<Item> ItemList;
    private Context context;
    private MyItemClickListener mItemClickListener;
    private MyItemLongClickListener mItemLongClickListener;

        public ItemAdapter(Context context,ArrayList<Item> ItemList) {
           this.context= context;
            this.ItemList = ItemList;

        }

        @Override
        public ListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, null);
            ListViewHolder mh = new ListViewHolder(v);

            return mh;
        }

        @Override
        public void onBindViewHolder(final ListViewHolder ListViewHolder, int i) {
            Item Item = ItemList.get(i);
            ListViewHolder.textName.setText(Item.getItem_name());
            ListViewHolder.textDesc.setText(Item.getItem_desc());
            ListViewHolder.textQty.setText(Item.getItem_qty());
            ListViewHolder.textName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        @Override
        public int getItemCount() {
            return  ItemList.size();
        }
    public class ItemsFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                if (constraint != null && constraint.length() > 0) {
                    ArrayList<Item> filterList = new ArrayList<Item>();
                    for (int i = 0; i < mStringFilterList.size(); i++) {
                        if ((mStringFilterList.get(i).getItem_name())
                                .contains(constraint.toString())) {

                            Item item = new Item(mStringFilterList.get(i)
                                    .getItem_name(), mStringFilterList.get(i)
                                    .getItem_desc(), mStringFilterList.get(i)
                                    .getItem_qty());

                            filterList.add(item);
                        }
                    }
                    results.count = filterList.size();
                    results.values = filterList;
                } else {
                    results.count = mStringFilterList.size();
                    results.values = mStringFilterList;
                }
                return results;

            }

            @Override
            public void publishResults(CharSequence constraint,
                                       Filter.FilterResults results) {
                ItemList = (ArrayList<Item>) results.values;
                notifyDataSetChanged();
            }


        }
    @Override
    public Filter getFilter() {
        // return a filter that filters data based on a constraint

        if (itemFilter == null) {
            itemFilter = new ItemsFilter();
        }
        return itemFilter;
    }
}

只需初始化项目列表,如下所示:

  private ArrayList<Item> ItemList = new ArrayList<Item>();
已编辑

    @Override
    public int getItemCount() {
        if (ItemList == null)
           return 0;
        else
           return  ItemList.size();
    }

ItemList
正在某处设置为
null
。防止这种情况的一种方法是,如果列表为空,则将
getItemCount
方法更改为返回0:

int getItemCount(){
    return  ItemList == null ? 0 : ItemList.size();
}

不要只是粘贴代码。实际发生的
NullPointerExcepton
在哪里?通过注释提及它。哪一行引发异常?我已经提到。。返回ItemList.size()-------此行抛出异常如果抛出异常,则表示
ItemList
为空。在调用
getItemCount
(并向其传递非null的
ArrayList
之前,是否调用构造函数
ItemAdapter
)?公共ItemAdapter(上下文上下文,ArrayList ItemList){this.Context=Context;this.ItemList=ItemList;}-------------------------------------这是我不知道这是否是我们的目标。如果你看一下构造函数,你会注意到它将一个
ArrayList
作为参数,然后他将
ItemList
变量链接到该参数中的ItemList,该变量可能为null,并且它永远无法实际初始化正在使用的ItemList。这正是我所想的。因此,OP应该确保检查传入构造函数的内容是否为空,而不是创建新的
ArrayList
,与其将一个新的
ArrayList
设为空,并假设它将被重写。@dhun---语法正确吗?
if
应该是小写的。我没有任何列表可供搜索。在act.adapter=new ItemAdapter(getActivity(),act.array_data)中,您能验证act.array_数据是否为空。Item cnt=new Item();cnt.setID(idno);cnt.setItem_名称(名称);cnt.setItem_desc(desc);cnt.setItem_数量(数量);act.array_data.add(cnt);------------是,因为act.array_数据包含列表,因为我可以查看列表中的项目。
    @Override
    public int getItemCount() {
        If (ItemList == null)
           return 0;
        else
           return  ItemList.size();
    }
    @Override
    public int getItemCount() {
        if (ItemList == null)
           return 0;
        else
           return  ItemList.size();
    }
int getItemCount(){
    return  ItemList == null ? 0 : ItemList.size();
}