-3

When i try to run my app the error shows that you have init the adapter i already init it and after i init it. it shows that reyclerview must be also init but as i do that already i don't get a solution please do helpful Thanks in Advance.

The Code is below

 package com.example.itemgetset
    
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    lateinit var activity: Activity
    val userList = ArrayList<ProductInfoGetSet>()
    lateinit var btnProductAdd: Button
    lateinit var llEmptyView: LinearLayout
    lateinit var llMain: LinearLayout
    lateinit var recyclerView: RecyclerView
    lateinit var llFab: LinearLayout
    lateinit var linearLayoutManager: LinearLayoutManager
    lateinit var gridLayoutManager: GridLayoutManager
    lateinit var adapter: CustomAdapter

    companion object {
        var handler: Handler = Handler()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.layoutManager = linearLayoutManager
        gridLayoutManager = GridLayoutManager(this, 2)
        recyclerView.layoutManager = gridLayoutManager
        activity = this
        initView()
        onClicks()
        setUpData()

        if (this::adapter.isInitialized)

            handler = @SuppressLint("HandlerLeak")
            object : Handler() {
                override fun handleMessage(msg: Message) {
                    if (msg.what == 111) {
                        var temp = Temp()
                        temp = msg.obj as Temp

                        if (temp.id == "") {
                            userList.add(
                                ProductInfoGetSet(
                                    (userList.size + 1).toString(),
                                    temp.name,
                                    temp.quantity,
                                    temp.price,
                                )
                            )
                            adapter = CustomAdapter(activity, userList)
                            recyclerView.adapter = adapter
                        } else {
                            for (i in userList.indices) {
                                if (userList[i].id == temp.id) {
                                    userList[i].id = temp.id
                                    userList[i].name = temp.name
                                    userList[i].quantity = temp.quantity
                                    userList[i].price = temp.price
                                }
                            }
                            adapter.notifyDataSetChanged()
                        }
                    }
                    if (userList.size > 0) {
                        llEmptyView.visibility = View.GONE
                        llMain.visibility = View.VISIBLE
                    } else {
                        llEmptyView.visibility = View.VISIBLE
                        llMain.visibility = View.GONE
                    }
                }
            }
    }

    private fun initView() {
        btnProductAdd = findViewById(R.id.btn_product_add)
        llFab = findViewById(R.id.ll_fab)
        llEmptyView = findViewById(R.id.llEmptyView)
        llMain = findViewById(R.id.llMain)
        recyclerView = findViewById(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
    }

    private fun onClicks() {
        btnProductAdd.setOnClickListener {
            val intent = Intent(this@MainActivity, AddDetails::class.java)
            intent.putExtra("isFor", "Add")
            startActivity(intent)
        }
        llFab.setOnClickListener {
            val intent = Intent(this@MainActivity, AddDetails::class.java)
            intent.putExtra("isFor", "Add")
            startActivity(intent)
        }
    }

    private fun setUpData() {
        if (userList.size > 0) {
            llEmptyView.visibility = View.GONE
            llMain.visibility = View.VISIBLE
        } else {
            llEmptyView.visibility = View.VISIBLE
            llMain.visibility = View.GONE
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.menu_view) {
            changeLayoutManager()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    private fun changeLayoutManager() {
        if (recycler_view.layoutManager == linearLayoutManager) {
            recycler_view.layoutManager = gridLayoutManager
            if (userList.size != 1) return
        } else {
            recycler_view.layoutManager = linearLayoutManager
        }
    }
}

And The error which was show is below

  Process: com.example.itemgetset, PID: 17453
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itemgetset/com.example.itemgetset.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property recyclerView has not been initialized
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3385)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3524)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2131)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7707)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property recyclerView has not been initialized
        at com.example.itemgetset.MainActivity.onCreate(MainActivity.kt:43)
        at android.app.Activity.performCreate(Activity.java:7825)
        at android.app.Activity.performCreate(Activity.java:7814)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1325)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3360)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3524) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2131) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7707) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

the line 43 is recyclerView.layoutManager = gridLayoutManager this and i already init it. Please do helpfull Thanks in advance

Jay Mistry
  • 15
  • 9
  • You should either initialize the variable in onCreate before everything runs, or use lazy initialization: `val recyclerView: RecyclerView by lazy { findViewById(R.id.recycler_view) }` – Animesh Sahu Sep 08 '20 at 04:46
  • i try that also but when i do that then after also an error came with that lateinit property adapter has not been initialized. and after i initialized it separately then after no item will display in my reyclerview – Jay Mistry Sep 08 '20 at 05:25
  • You have to define those... declaring them as lateinit doesn't make them hold a reference... – Animesh Sahu Sep 08 '20 at 05:29
  • Can you please give a example i don't get it.Thanks – Jay Mistry Sep 08 '20 at 05:46

2 Answers2

1

You didn't initialize "recyclerView" itself. You probably should do it as recyclerView = findViewById(R.id.recyclerView) or remove the lateinit var recyclerView from the top of the class. The variable might conflict with the view itself which comes directly from your layout.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
  • After i remove it from the top it show the error below everywhere where the recycler_view is used and i also do this like 'val recyclerView = findViewById(R.id.recyclerView)' but not done. still error occurs – Jay Mistry Sep 07 '20 at 12:45
  • Move the `initView()` function right below `setContentView()` and add the `lateinit var recyclerView: RecyclerView` back to the top of the class then. `initView()` is called after you try to access your views, it should be called before. – Furkan Yurdakul Sep 07 '20 at 12:50
0

Few Things to notice here

  1. You have used every property as lateinit
  2. initialze view just after setContentView(...)

Try This:

    class MainActivity : AppCompatActivity() {
    
        lateinit var activity: Activity
        val userList = ArrayList<ProductInfoGetSet>()
        lateinit var btnProductAdd: Button
        lateinit var llEmptyView: LinearLayout
        lateinit var llMain: LinearLayout
        lateinit var recyclerView: RecyclerView
        lateinit var llFab: LinearLayout
        lateinit var linearLayoutManager: LinearLayoutManager
        lateinit var gridLayoutManager: GridLayoutManager
        lateinit var adapter: CustomAdapter
    
        companion object {
            var handler: Handler = Handler()
        }
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            initView()
            linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
            recyclerView.layoutManager = linearLayoutManager
            gridLayoutManager = GridLayoutManager(this, 2)
            recyclerView.layoutManager = gridLayoutManager
            activity = this
            onClicks()
            setUpData()
    
            if (this::adapter.isInitialized)
    
                handler = @SuppressLint("HandlerLeak")
                object : Handler() {
                    override fun handleMessage(msg: Message) {
                        if (msg.what == 111) {
                            var temp = Temp()
                            temp = msg.obj as Temp
    
                            if (temp.id == "") {
                                userList.add(
                                    ProductInfoGetSet(
                                        (userList.size + 1).toString(),
                                        temp.name,
                                        temp.quantity,
                                        temp.price,
                                    )
                                )
                                adapter = CustomAdapter(activity, userList)
                                recyclerView.adapter = adapter
                            } else {
                                for (i in userList.indices) {
                                    if (userList[i].id == temp.id) {
                                        userList[i].id = temp.id
                                        userList[i].name = temp.name
                                        userList[i].quantity = temp.quantity
                                        userList[i].price = temp.price
                                    }
                                }
                                adapter.notifyDataSetChanged()
                            }
                        }
                        if (userList.size > 0) {
                            llEmptyView.visibility = View.GONE
                            llMain.visibility = View.VISIBLE
                        } else {
                            llEmptyView.visibility = View.VISIBLE
                            llMain.visibility = View.GONE
                        }
                    }
                }
        }
.........
....
}
chand mohd
  • 2,363
  • 1
  • 14
  • 27