android-getItemCount中的RecyclerView NullPointerException问题

android-getItemCount中的RecyclerView NullPointerException问题,第1张

概述请调查一下,让我知道出了什么问题.publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.ViewHolder>{privateItemData[]itemsData;publicMyAdapter(ItemData[]itemsData){this.itemsData=itemsData;}publicMyAdapter(String

请调查一下,让我知道出了什么问题.

public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.VIEwHolder> {    private ItemData[] itemsData;    public MyAdapter(ItemData[] itemsData) {        this.itemsData = itemsData;    }    public MyAdapter(String[] Titles, int[] icons, String name, String email, int profile) {}    // Create new vIEws (invoked by the layout manager)    @OverrIDe    public MyAdapter.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,        VIEw itemLayoutVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab1List, null);        // create VIEwHolder        VIEwHolder vIEwHolder = new VIEwHolder(itemLayoutVIEw);        return vIEwHolder;    }    // Replace the contents of a vIEw (invoked by the layout manager)    @OverrIDe    public voID onBindVIEwHolder(VIEwHolder vIEwHolder, int position) {        // - get data from your itemsData at this position        // - replace the contents of the vIEw with that itemsData        vIEwHolder.txtVIEwTitle.setText(itemsData[position].getTitle());        vIEwHolder.imgVIEwIcon.se@R_403_5411@Resource(itemsData[position].ge@R_403_5411@Url());    }    // inner class to hold a reference to each item of RecyclerVIEw    public static class VIEwHolder extends RecyclerVIEw.VIEwHolder {        public TextVIEw txtVIEwTitle;        public ImageVIEw imgVIEwIcon;        public VIEwHolder(VIEw itemLayoutVIEw) {            super(itemLayoutVIEw);            txtVIEwTitle = (TextVIEw) itemLayoutVIEw.findVIEwByID(R.ID.abb);            imgVIEwIcon = (ImageVIEw) itemLayoutVIEw.findVIEwByID(R.ID.baa);        }    }    // Return the size of your itemsData (invoked by the layout manager)    @OverrIDe    public int getItemCount() {        return itemsData.length;// here im getting the `null` pointer exception error    }}

返回itemsData.length;的空指针异常;我需要在一个寻呼机或一个滑动的布局中设计一个ListvIEw.但我收到此错误.

解决方法:

传递给MyAdapter的构造函数的itemsData参数为null,或者您使用的第二个构造函数未初始化itemsData类成员.您可以检查它是否为null并从getItemCount方法返回0:

@OverrIDepublic int getItemCount() {    return itemsData == null ? 0 : itemsData.length;}
总结

以上是内存溢出为你收集整理的android-getItemCount中的RecyclerView NullPointerException问题全部内容,希望文章能够帮你解决android-getItemCount中的RecyclerView NullPointerException问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存