简体   繁体   English

RecyclerViewAdapter 的 onBindViewHolder() 出错

[英]Error in RecyclerViewAdapter's onBindViewHolder()

I have a problem in the onBindViewHolder method in my adapter.我的适配器中的 onBindViewHolder 方法有问题。 I want to create a scrollable list of items (each item is composed by an image and 3 text fields) that grabs its information from a random API that I chose and displays it.我想创建一个可滚动的项目列表(每个项目由一个图像和 3 个文本字段组成),它从我选择的随机 API 中获取其信息并显示它。 The problem is that the onBindViewHolder method lets me create the list of items only with the imatge and the first textview, but when it starts executing the second text view it crashes and I don't know why is happennig that.问题是 onBindViewHolder 方法让我只使用 imatge 和第一个 textview 创建项目列表,但是当它开始执行第二个文本视图时它崩溃了,我不知道为什么会发生这种情况。 If anyone knows the answer I would be very happy, thanks!如果有人知道答案,我会很高兴,谢谢!

LogCat日志猫

    --------- beginning of crash
2021-03-29 16:48:21.353 2548-2548/com.example.prova2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prova2, PID: 2548
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:339)
        at android.widget.TextView.setText(TextView.java:5496)
        at com.example.prova2.ItemAdapter.onBindViewHolder(ItemAdapter.java:45)
        at com.example.prova2.ItemAdapter.onBindViewHolder(ItemAdapter.java:21)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
.
.
. etc

Adapter File适配器文件

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item i = dataset.get(position);
        holder.totalTextView.setText(i.getId());
        Log.e(TAG, "holder.getId fet: " + i.getUser_id());
        holder.totalHitsTextView.setText(i.getFavorites());
        holder.hitsView.setText(i.getImageHeight());

        String url = i.getUserImageURL();
        Glide.with(mContext).load(url).centerCrop().
                diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.fotoImageView);

        holder.parent_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

change holder.totalTextView.setText(Si.getId());更改holder.totalTextView.setText(Si.getId()); to holder.totalTextView.setText(String.valueOf(Si.getId()));holder.totalTextView.setText(String.valueOf(Si.getId())); and holder.hitsView.setText(i.getImageHeight());holder.hitsView.setText(i.getImageHeight()); to holder.hitsView.setText(String.valueOf(i.getImageHeight()))holder.hitsView.setText(String.valueOf(i.getImageHeight()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM