Home > database >  How to solve UninitializedPropertyAccessException when setting clickListener to an ImageButton?
How to solve UninitializedPropertyAccessException when setting clickListener to an ImageButton?

Time:08-15

I know that already been questions about this error but i cant find something similar to this case.

I'm randomly getting this exception. Most of the time it works ok, but from time-to-time I get this exception. How can I work it out?

 Caused by kotlin.UninitializedPropertyAccessException: lateinit property btnMenu has not been initialized
   at com.xxx.yyy.base.BaseFragment.onViewCreated(BaseFragment.kt:154)
   at com.xxx.yyy.presentation.furnitures.view.FurnituresAddFragment.onViewCreated(FurnituresAddFragment.kt:106)
   at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
   at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
   at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
   at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
   at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
   at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
   at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
   at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
   at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:392)
   at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
   at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
   at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
   at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
   at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
   at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1073)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1001)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
   at com.xxx.yyy.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:215)
   at com.xxx.yyy.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:209)
   at com.xxx.yyy.base.MainActivity.onCreate(MainActivity.kt:114)
   at android.app.Activity.performCreate(Activity.java:8232)
   at android.app.Activity.performCreate(Activity.java:8212)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1331)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3724)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3919)
   at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
   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:2311)
   at android.os.Handler.dispatchMessage(Handler.java:111)
   at android.os.Looper.loopOnce(Looper.java:238)
   at android.os.Looper.loop(Looper.java:355)
   at android.app.ActivityThread.main(ActivityThread.java:8080)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)

The btnMenu is an ImageButton in my activity_main.xml. Then my BaseFragment.kt class has this code:

 private lateinit var btnMenu: ImageButton

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    basePresenter = BasePresenter()

    coroutineScope = CoroutineScope(IO)
    FirebaseFirestore.setLoggingEnabled(true)

    btnMenu.setOnClickListener {
        ....
    }
}

CodePudding user response:

You are trying to access button without initializing. You need to initialize it in onviewCreated.

btnMenu = view.findViewById(R.id.btn_id) //Your button Id

private lateinit var btnMenu: ImageButton

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    **//You need to initialize btnMenu first before using.**
    btnMenu = view.findViewById(R.id.btn_id) //here
    basePresenter = BasePresenter()

    coroutineScope = CoroutineScope(IO)
    FirebaseFirestore.setLoggingEnabled(true)

    btnMenu.setOnClickListener {
        ....
    }
}
  • Related