Construct a Fragment | raywenderlich.com


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

Help please :slight_smile: After deleting the FragmentLayout in content_main.xml and replacing it with the <fragment tag, my app no longer saves my ToDoLists and now crashes when I try to add a new list right after tapping the “Create” button.

Hey there, sorry to hear that. The first approach is to diagnose the issue. Find out why it is crashing. In Android Studio, open LogCat, and search for the app name. You should see a stack trace which will let you know where the error occurred.

My guess is that the app is trying to access a id that is no longer there. Check out the stack trace and see what’s written. Feel free to paste it here.

I am facing the same error. After deleting FrameLayout, the app is crashing. Follow below the issue:

2020-06-03 15:33:42.647 9015-9015/com.example1234.todo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example1234.todo, PID: 9015
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123, result=-1, data=Intent { (has extras) }} to activity {com.example1234.todo/com.example1234.todo.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property listDataManager has not been initialized
at android.app.ActivityThread.deliverResults(ActivityThread.java:4360)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
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)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property listDataManager has not been initialized
at com.example1234.todo.TodoListFragment.saveList(TodoListFragment.kt:75)
at com.example1234.todo.MainActivity.onActivityResult(MainActivity.kt:91)
at android.app.Activity.dispatchActivityResult(Activity.java:7454)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
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)

It’s hard to say what is going on without looking at your code, but the error appears to be related to the ListDataManager. It’s not being created and thus causing the runtime error.

Hi There!

Not sure if this is the same issue, but I realize that after adding the fragment directly to the layout, I end up with TWO instances of TodoListFragment floating around: one that was attached to the layout, and one that is instantiated directly by MainActivity. The reference held by MainActivity is NOT to the real fragment displayed in the UI, and never has onAttach called, so my listDataManager is never initialized (which is the error I was getting). I modified my MainActivity to grab a reference to the fragment by adding the following to onCreate:

supportFragmentManager.findFragmentById(R.id.todo_list_fragment)
        ?.let { todoListFragment = it as TodoListFragment }

Is this the best practice for getting a reference to a fragment defined in a layout, or is there a better way? Thanks!

I had the same issue here… not sure what’s going on but changing to use the tag over the fragment manager causes it to not call onAttach which means my listDataManager is never set… Looking at the replies on this it looks dead but it would be nice to get an answer on why this is happening.

Hi Jordan, can you send me your project files? My email is brian@razeware.com. I’ll take a look and see what I can find.

@ericaddison Do you still have issues with this?

@shogunkaramazov I’m having the same issue, the first lines of my logcat are:

2022-03-14 10:02:15.045 9400-9400/com.accreditaire.listmaker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.accreditaire.listmaker, PID: 9400
kotlin.UninitializedPropertyAccessException: lateinit property listDataManager has not been initialized
at com.accreditaire.listmaker.TodoListFragment.addList(TodoListFragment.kt:68)

Hello! Any way around this error?

2022-03-23 08:53:01.944 6830-6830/com.raywenderlich.listmaker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raywenderlich.listmaker, PID: 6830
kotlin.UninitializedPropertyAccessException: lateinit property listDataManager has not been initialized
at com.raywenderlich.listmaker.TodoListFragment.addList(TodoListFragment.kt:69)

Same error with the course’s 36th lesson Github project