我的setimageresource有一个致命的例外,我不明白为什么

发布于 2025-02-04 21:43:39 字数 2737 浏览 4 评论 0原文

我正在使用Kotlin进行学校项目构建BoardGame Manager应用程序,而我却擅长使用Kotlin和Android Studio。我决定根据我的需求调整一个在线教程,以便可以构建自己的应用程序。但是,当我启动该应用程序时出现此错误,而我不明白为什么:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

我尝试在调试模式下启动它,因此我可能会发现问题,但我不明白为什么它可以提及它可以是无效的。

我使用实时firebase数据库,这是我的bgadapter的代码,在那里出现致命的例外:

class BgAdapter(
val context: MainActivity,
private val bgList: List<BgModel>,
private val layoutId: Int) : RecyclerView.Adapter<BgAdapter.ViewHolder>() {

//Boîte pour ranger tout les composants à contrôler
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val bgImage = view.findViewById<ImageView>(R.id.image_item)
    val bgName: TextView? = view.findViewById(R.id.name_item)
    val bgEditor: TextView? = view.findViewById(R.id.editor_item)
    val bgFavIcon = view.findViewById<ImageView>(R.id.add_light_icon)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater
        .from(parent.context)
        .inflate(layoutId, parent, false)

    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    //Récupérer les informations des bgs
    val currentBg = bgList[position]

    //Récupérer le repository
    val repo = BgRepository()

    //Utiliser glide pour récupérer l'image à partir de son lien -> composant
    Glide.with(context).load(Uri.parse(currentBg.image_url)).into(holder.bgImage)

    //Mettre à jour le nom du jeu
    holder.bgName?.text = currentBg.name

    //Mettre à jour le nom de l'éditeur du jeu
    holder.bgEditor?.text = currentBg.editor

    //Vérifier si le jeu a été ajouté ou non
    if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

    //Rajouter une interaction sur icône checkbox
    holder.bgFavIcon.setOnClickListener {
        //Inverse si le bouton est en mode "ajouté" ou non
        currentBg.faved = !currentBg.faved
        //Mettre à jour l'objet jeu
        repo.updateBg(currentBg)
    }

    //Interaction lors du clic sur un jeu
    holder.itemView.setOnClickListener {
        //Afficher la popup
        BgInfoPopup(this, currentBg).show()
    }
}

override fun getItemCount(): Int = bgList.size

}

致命的例外是针对特定的部分:

        if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

如果您对真正需要帮助的无经验的学生有任何想法呢:)

谢谢你

I'm building a boardgame manager app using kotlin for a school project, and I'm new at using Kotlin and Android Studio. I've decided to adapt an online tutorial to my needs so I can build my own app. But this error appears when I launch the app and I don't understand why :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

I've tried to launch it in debug mode so I may find the problem but I don't understand why it is mentionned that it can be null.

I use a realtime firebase database, and here is the code of my BgAdapter, where the FATAL EXCEPTION appears :

class BgAdapter(
val context: MainActivity,
private val bgList: List<BgModel>,
private val layoutId: Int) : RecyclerView.Adapter<BgAdapter.ViewHolder>() {

//Boîte pour ranger tout les composants à contrôler
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val bgImage = view.findViewById<ImageView>(R.id.image_item)
    val bgName: TextView? = view.findViewById(R.id.name_item)
    val bgEditor: TextView? = view.findViewById(R.id.editor_item)
    val bgFavIcon = view.findViewById<ImageView>(R.id.add_light_icon)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater
        .from(parent.context)
        .inflate(layoutId, parent, false)

    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    //Récupérer les informations des bgs
    val currentBg = bgList[position]

    //Récupérer le repository
    val repo = BgRepository()

    //Utiliser glide pour récupérer l'image à partir de son lien -> composant
    Glide.with(context).load(Uri.parse(currentBg.image_url)).into(holder.bgImage)

    //Mettre à jour le nom du jeu
    holder.bgName?.text = currentBg.name

    //Mettre à jour le nom de l'éditeur du jeu
    holder.bgEditor?.text = currentBg.editor

    //Vérifier si le jeu a été ajouté ou non
    if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

    //Rajouter une interaction sur icône checkbox
    holder.bgFavIcon.setOnClickListener {
        //Inverse si le bouton est en mode "ajouté" ou non
        currentBg.faved = !currentBg.faved
        //Mettre à jour l'objet jeu
        repo.updateBg(currentBg)
    }

    //Interaction lors du clic sur un jeu
    holder.itemView.setOnClickListener {
        //Afficher la popup
        BgInfoPopup(this, currentBg).show()
    }
}

override fun getItemCount(): Int = bgList.size

}

The FATAL EXCEPTION is targetting specificaly this part :

        if (currentBg.faved) {
        holder.bgFavIcon.setImageResource(R.drawable.ic_check)
    } else {
        holder.bgFavIcon.setImageResource(R.drawable.ic_add_light)
    }

If you have any idea for an unexperienced student who really needs help, feel free to answer ! :)

Thank you by advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2025-02-11 21:43:39

我认为您可能需要检查将您传递到bgadapter中的布局,以查看是否包括R.Id.add_light_icon

I think you might need to check the layoutId you passed into the BgAdapter to see if R.id.add_light_icon is included

黑凤梨 2025-02-11 21:43:39

您必须在布局中检查r.id.add_light_icon
如果您的代码中没有错,则可能是您正在夸大错误的布局,并且获取零视图可能会有您的布局或传递错误的ID中没有视图。

You have to check the R.id.add_light_icon in your layout.
If there is nothing wrong in your code may be you are inflating wrong layout and getting null view may be there is no view present in your layout or passing wrong id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文