Search code examples
androidruntime-errorlogcatfatal-errorpid

"AppName" Has Stopped . A dialog message popup when I run the my App


When I try to execute my app, the app first gets into the white screen then the message popup that "App" Has Stopped . I tried to solve the problem but the issue remains the same. for this particular snippet of code.

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)

 }

main Activity

//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

    }
}

This is the Logcat Error I faced

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 ----------------------------

The Error that is shown on the Logcat and currently I am on the Koala version. Help me to solve the problem


Solution

  • lateinit property binding has not been initialized

    your binding variable was not initialized when setContentView(binding.root) was called in onCreate()