Issue
This crash has been showing up in my developer console for a long time and I have not been able to reproduce it in any way.
I have an app that starts with MainActivity. In onCreate, some data is downloaded asynchronously from the backend. When pressing a button in this activity, a dialog is opened that shows some data downloaded from the server.
This is the crash:
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.******.******/com.******.******.MainActivity}: com.******.******.users.UserData$DataNotFoundException: Requested element not found
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6942)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by com.******.******.users.UserData$DataNotFoundException: Requested element not found
at com.******.******.users.UserData.getData(UserData.java:478)
at com.******.******.UserDialog.onCreateDialog(UserDialog.java:87)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:380)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1412)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1341)
at android.app.Activity.performStart(Activity.java:7200)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6942)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
What is confusing to me is that the crash happens in performLaunchActivity because of a crash while the dialog is being inflated. However, the dialog is not created in the activity's onCreate, but only after the user presses a button. The button however is not being clicked on (I added a log in the click listener and it is never called).
I have seen crashes with similar patterns in GitHub issue trackers but with no replies, so this seems like a common pattern but hard to debug.
The question is in what case the Dialog can be instantiated from performLaunchActivity even though there is no reference to the dialog in the onCreate() method. I am guessing it is related to the activities' lifecycle but I have not been able to crack it out.
Solution
Your crash is probably happening when the screen rotates, or the app is restored after a process death in the background. In that case the DialogFragment
is created without user interaction. Have you checked and reasoned about those scenarios?
You probably want to pass the data as an argument when opening the dialog as per documentation. That way, the system will recreate the dialog with the data intact.
Answered By - Steve M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.