Your Second Kotlin Android App - Part 27: Integrating | Ray Wenderlich

This video will show you the process of replacing activity code with a fragment instead.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4534-your-second-kotlin-android-app/lessons/27

Hello @bdmoakley ! I’ve an issue after Integrating Fragments into Activities.

When I run my app,

  • I click the add button to open Dialog to create a list
  • when I click on Create, my app crashes
  • in Logcat I can see this error
2019-02-28 10:51:38.328 3672-3672/com.example.listmaker E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.listmaker, PID: 3672
    kotlin.UninitializedPropertyAccessException: lateinit property listDataManager has not been initialized
        at com.example.listmaker.ListSelectionFragment.addList(ListSelectionFragment.kt:74)
        at com.example.listmaker.MainActivity$showCreateListDialog$1.onClick(MainActivity.kt:62)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I compared my code with the final code but found no differences…

I tried to put breakpoints on my ListSelectionFragment.onAttach method :

override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnFragmentInteractionListener) {
            listener = context
            listDataManager = ListDataManager(context)
        } else {
            throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
        }
    }

but it does not go into it ???

Any ideas ?

Tx

PS > to emulate, I have a device on API 28

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hello Cecilem and Shogunkaramazov!

You are not the only ones who got stuck in this part, because in this lesson it seems that everything was done.

However it was missing to attach the Fragment to MainActivity via XML and Kotlin code.

Watch the lesson “Showing the Fragment” and you will find the answer.

Best regards.