简体   繁体   English

我的代码给了我这个错误有人可以帮助我吗

[英]my code is giving me this error can someone help me out

2022-12-25 23:19:31.550 11731-11731/com.example.gonews E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.gonews, PID: 11731 android.content.res.Resources$NotFoundException: Resource ID #0x7f08015a type #0x12 is not valid 2022-12-25 23:19:31.550 11731-11731/com.example.gonews E/AndroidRuntime:致命异常:主进程:com.example.gonews,PID:11731 android.content.res.Resources$NotFoundException:资源 IDNotFoundException #0x7f08015a 类型 #0x12 无效

package com.example.gonews

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class Newsadaptor(val i:ArrayList<String>): RecyclerView.Adapter<newsview>() {
    @SuppressLint("ResourceType")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): newsview {
     val view=LayoutInflater.from(parent.context).inflate(R.id.rview,parent,false)
     return newsview(view)
    }

    override fun onBindViewHolder(holder: newsview, position: Int) {
    val crritem=i[position]
    holder.t.text=crritem
    }

    override fun getItemCount(): Int {
        return i.size
    }
}

class newsview(itemView: View) : RecyclerView.ViewHolder(itemView) {
val t:TextView=itemView.findViewById(R.id.t1)
}

my android ap is not opening but gradle build is happening我的 android ap 没有打开,但是 gradle 正在构建

In the onCreateViewHolder() method you need to inflate the layout you want to show in the recycler view.onCreateViewHolder()方法中,您需要膨胀要在回收站视图中显示的布局。 You are inflating a view instead of a layout, that's why it is giving the exception.您正在膨胀视图而不是布局,这就是它给出异常的原因。

Instead of inflating R.id.rvview which I believe is a view or maybe your recycler view, you need to inflate the layout you want to see in the list.而不是膨胀R.id.rvview我认为它是一个视图或者你的回收者视图,你需要膨胀你想在列表中看到的布局。

so, you need to replace this line:所以,你需要替换这一行:

val view=LayoutInflater.from(parent.context).inflate(R.id.rview,parent,false)

to this:对此:

val view=LayoutInflater.from(parent.context).inflate(R.layout.your_item_layout,parent,false)

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

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