简体   繁体   中英

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

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

In the onCreateViewHolder() method you need to inflate the layout you want to show in the recycler view. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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