适配器崩溃的零指针异常在生产构建中的Firebase Analytics中显示

发布于 2025-02-10 17:14:11 字数 4103 浏览 1 评论 0原文

我在适配器中得到了无效的指针例外,并且无法弄清楚我的代码出了什么问题。它在测试构建方面的工作正常,但在生产构建中会导致Firebase Analytics崩溃。

适配器代码是:

   
class NotificationAdapter(notificationDataArrayList:ArrayList<NotificationsData>, private val clickListener:ClickListener,
                          mcontext: Context):
        RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder>()
    {
        val notificationArrayList : ArrayList<NotificationsData>
        val mcontext : Context

        inner class NotificationViewHolder (itemView: View): RecyclerView.ViewHolder(itemView){


            val tv_notification_title : TextView
            val tv_notification_body : TextView
//            val expandButton : ImageButton
            val expensionBody: LinearLayout

            init {
                tv_notification_title = itemView.findViewById(R.id.til_request)
                tv_notification_body = itemView.findViewById(R.id.tv_notification_body)
//             expandButton = itemView.findViewById(R.id.btn_expand)
                expensionBody = itemView.findViewById(R.id.layoutExpand)

            }
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int,
        ): NotificationViewHolder {
            val view : View = LayoutInflater.from(parent.context).inflate(R.layout.ui_notification_item,parent,false)
            return NotificationViewHolder(view)

        }

        override fun onBindViewHolder(
            holder:NotificationViewHolder,
            position: Int
        ) {
            val recyclerData : NotificationsData = notificationArrayList[position]
            holder.tv_notification_title.text = recyclerData.Title
            holder.tv_notification_body.text = recyclerData.Body
            holder.itemView.setOnClickListener {
                clickListener.onItemClick(notificationArrayList[position])

            }

        }

        override fun getItemCount(): Int {
            return notificationArrayList.size
        }
        init {
            notificationArrayList = notificationDataArrayList
            this.mcontext = mcontext
        }

        interface ClickListener{
            fun onItemClick(dataModel: NotificationsData)
        }
    }
and I use in adapter in main fragment like :

  recyclerView = binding.notificationRv.apply {
                                        adapter = NotificationAdapter(response.body()!!.Data,this@NotificationsFragmentUI,context)
                                        layoutManager = manager
                                        adapter = adapter

在这个问题上,我被困了2天,无法弄清楚如何修复它?

firebase崩溃的路是:

Fatal Exception: java.lang.NullPointerException
Parameter specified as non-null is null: method kotlin.jvm.internal.j.e, parameter notificationDataArrayList
com.ls.wo.sm.ui.notifications.NotificationAdapter.<init> (NotificationAdapter.kt:17)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1$onResponse$1.invoke (NotificationsFragmentUI.kt:100)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1$onResponse$1.invoke (NotificationsFragmentUI.kt:76)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI.checkIfFragmentAttached (NotificationsFragmentUI.kt:130)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1.onResponse (NotificationsFragmentUI.kt:76)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0 (DefaultCallAdapterFactory.java:89)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.$r8$lambda$hVGjmafRi6VitDIrPNdoFizVAdk (DefaultCallAdapterFactory.java)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$$InternalSyntheticLambda$0$daf5173ad73a9dc0e8131143f299c222e2ca4cddd7cb10b2d0a6e331a0602ae1$0.run (DefaultCallAdapterFactory.java:6)

I get null pointer exception in adapter and can't figure it out what is wrong with my code. Its working fine in test builds but in production build it give crash in firebase analytics.

Adapter code is:

   
class NotificationAdapter(notificationDataArrayList:ArrayList<NotificationsData>, private val clickListener:ClickListener,
                          mcontext: Context):
        RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder>()
    {
        val notificationArrayList : ArrayList<NotificationsData>
        val mcontext : Context

        inner class NotificationViewHolder (itemView: View): RecyclerView.ViewHolder(itemView){


            val tv_notification_title : TextView
            val tv_notification_body : TextView
//            val expandButton : ImageButton
            val expensionBody: LinearLayout

            init {
                tv_notification_title = itemView.findViewById(R.id.til_request)
                tv_notification_body = itemView.findViewById(R.id.tv_notification_body)
//             expandButton = itemView.findViewById(R.id.btn_expand)
                expensionBody = itemView.findViewById(R.id.layoutExpand)

            }
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int,
        ): NotificationViewHolder {
            val view : View = LayoutInflater.from(parent.context).inflate(R.layout.ui_notification_item,parent,false)
            return NotificationViewHolder(view)

        }

        override fun onBindViewHolder(
            holder:NotificationViewHolder,
            position: Int
        ) {
            val recyclerData : NotificationsData = notificationArrayList[position]
            holder.tv_notification_title.text = recyclerData.Title
            holder.tv_notification_body.text = recyclerData.Body
            holder.itemView.setOnClickListener {
                clickListener.onItemClick(notificationArrayList[position])

            }

        }

        override fun getItemCount(): Int {
            return notificationArrayList.size
        }
        init {
            notificationArrayList = notificationDataArrayList
            this.mcontext = mcontext
        }

        interface ClickListener{
            fun onItemClick(dataModel: NotificationsData)
        }
    }

and I use in adapter in main fragment like :

  recyclerView = binding.notificationRv.apply {
                                        adapter = NotificationAdapter(response.body()!!.Data,this@NotificationsFragmentUI,context)
                                        layoutManager = manager
                                        adapter = adapter

I am stuck for 2 days on this issue can't figure out how to fix it?

firebase crashlatics are:

Fatal Exception: java.lang.NullPointerException
Parameter specified as non-null is null: method kotlin.jvm.internal.j.e, parameter notificationDataArrayList
com.ls.wo.sm.ui.notifications.NotificationAdapter.<init> (NotificationAdapter.kt:17)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1$onResponse$1.invoke (NotificationsFragmentUI.kt:100)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1$onResponse$1.invoke (NotificationsFragmentUI.kt:76)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI.checkIfFragmentAttached (NotificationsFragmentUI.kt:130)
com.ls.wo.sm.ui.notifications.NotificationsFragmentUI$notificationCall$1.onResponse (NotificationsFragmentUI.kt:76)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0 (DefaultCallAdapterFactory.java:89)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.$r8$lambda$hVGjmafRi6VitDIrPNdoFizVAdk (DefaultCallAdapterFactory.java)
retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$InternalSyntheticLambda$0$daf5173ad73a9dc0e8131143f299c222e2ca4cddd7cb10b2d0a6e331a0602ae1$0.run (DefaultCallAdapterFactory.java:6)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文