gpt4 book ai didi

android - getItemCount 中的 RecyclerView NullPointerException 问题

转载 作者:太空狗 更新时间:2023-10-29 16:34:58 26 4
gpt4 key购买 nike

请调查一下,让我知道哪里出了问题。

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.setImageResource(itemsData[position].getImageUrl());
}

// 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 参数为空,或者您正在使用第二个未初始化 itemsData 类的构造函数成员。您可以检查它是否为 null 并从 getItemCount 方法返回 0:

@Override
public int getItemCount() {
return itemsData == null ? 0 : itemsData.length;
}

关于android - getItemCount 中的 RecyclerView NullPointerException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com