Group Group Group Group Group Group Group Group Group

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!