“AppName”已停止。当我运行我的应用程序时弹出对话框消息

问题描述 投票:0回答:1

当我尝试执行我的应用程序时,应用程序首先进入白屏,然后弹出消息“应用程序”已停止。我尝试解决该问题,但问题仍然存在。对于这个特定的代码片段。

package com.example.recycleviewscroller

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.recycleviewscroller.databinding.RvItemBinding

class RvAdapter(var datalist: ArrayList<RvModel>, var context: Context): RecyclerView.Adapter<RvAdapter.MyViewHolder>(){
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
//        var view = LayoutInflater.from(context).inflate(R.layout.rv_item,parent,false)
//         return MyViewHolder(view)
         var binding = RvItemBinding.inflate(LayoutInflater.from(context),parent,false)
         return MyViewHolder(binding)
     }

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

     override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding.profile.setImageResource(datalist[position].profile)
         holder.binding.hotelName.text = datalist[position].hotelName
         holder.binding.address.text = datalist[position].address
     }
     inner class MyViewHolder(var binding: RvItemBinding) : RecyclerView.ViewHolder(binding.root)

 }

主要活动

//Step 1 -> Add recyclerview to the xml
//Step 2 -> make itemView
//step 3 -> make data model
//Step 4 -> make adapter
//Step 5 -> data list
package com.example.recycleviewscroller

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.recycleviewscroller.databinding.ActivityMainBinding



class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var rvAdapter: RvAdapter
    private lateinit var dataList: ArrayList<RvModel>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets


        }
        dataList = ArrayList<RvModel>()
        binding = ActivityMainBinding.inflate(layoutInflater)
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))
        dataList.add(RvModel(R.drawable.food, "Andorus Hotel ", "Andra"))

        rvAdapter = RvAdapter(dataList, this)
        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = rvAdapter

    }
}

这是我遇到的Logcat错误

FATAL EXCEPTION: main (Ask Gemini)
                                                                                                    Process: com.example.recycleviewscroller, PID: 12370
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recycleviewscroller/com.example.recycleviewscroller.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
.........                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                                    Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
                                                                                                        
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
2024-07-27 10:09:52.381 16248-16259 art                     com.example.recycleviewscroller      I  Background sticky concurrent mark sweep GC freed 45348(1913KB) AllocSpace objects, 0(0B) LOS objects, 47% free, 4MB/8MB, paused 1.302ms total 160.099ms
2024-07-27 10:10:06.959 16248-16255 art                     com.example.recycleviewscroller      I  Debugger is no longer active
2024-07-27 10:10:06.959 16248-16255 art                     com.example.recycleviewscroller      I  Starting a blocking GC Instrumentation
---------------------------- PROCESS ENDED (16248) for package com.example.recycleviewscroller ----------------------------

Logcat 上显示的错误,目前我使用的是 Koala 版本。帮我解决问题

android runtime-error logcat fatal-error pid
1个回答
0
投票

lateinit 属性绑定尚未初始化

binding
在 onCreate() 中调用时,你的变量
setContentView(binding.root)
尚未初始化

© www.soinside.com 2019 - 2024. All rights reserved.