-1

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.

ig343
  • 277
  • 1
  • 3
  • 17
  • Pl share your code. Also check [this](https://stackoverflow.com/questions/3008745/android-activitythread-performlaunchactivity-error/3008862) if of any help. – Rajen Raiyarela May 09 '20 at 05:53
  • I can't share all of my code. I think there is enough info in the post. – ig343 May 09 '20 at 20:27
  • I know the rules. I am not trying to get someone to debug my code, the question is in what case it is possible that an activity crashes when launching because of a dialog that shouldn't be open. – ig343 May 10 '20 at 17:00
  • @ig343 can you share UserData java file – Srinivasan M May 11 '20 at 11:52
  • @ig343 Have you defined dialog in onResume or onStart method? – Nainal May 11 '20 at 12:22
  • no, the click listener is defined in a different method. that is why the issue is hard to debug – ig343 May 11 '20 at 22:22

5 Answers5

2

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.

Steve M
  • 9,296
  • 11
  • 49
  • 98
  • Thanks for the answer Steve. I check screen rotation and couldn't reproduce. Also tried enabling the developer option that kills activities when they are no longer visible, but also couldn't reproduce. The crashlytics logs show that RAM usage is not necessarily high when the crash occurs. Do you have any other ideas to try? – ig343 May 15 '20 at 05:11
  • 1
    @ig343 Don't keep activities is not process death. With the dialog open, hit the home button to put the app in the background. Then issue `adb shell am kill com.xyz.yourapp`. Then reenter app. – Steve M May 15 '20 at 13:43
1

If Android needs to recreate an activity when the dialog is shown, it will re-create the dialog when it recreates the activity. For example, your activity may be killed when it is in background to free memory for the foreground app.

In this case, your dialog may run into problems if it uses data in memory that existed before the activity was killed, but these data is not instantiated again with the dialog.

For this reasons, dialogs have arguments, which are preserved and passed to the dialog when it is recreated. Use setArguments(Bundle args) to pass the data to the dialog.

Rediska
  • 1,392
  • 10
  • 14
0

based on your stack trace my guess is you have function or used some kind of global variable that changes when your asynchronously get your data and then it crashes because your dialog is not inflated yet.

my best guess is that you have used DialogFragment and its lifecycle is a little bit different than regular fragment and it is trying to prepare the data that is going to be in the layout.

I am just guessing over here because your question is not clear enough but to debug and fix this issue I will suggest you change your getData method and put a constant data in it to see if the crash still happens, if crash stopped it is because of the variable you used and should handle your fragment and dialog lifecycle to fix the crash, and if crash still would happen change to regular fragment and just post what happens so I can know what is happening.

overall this happens when you are using some variable or populating some views with data that is not generated yet, it also may be caused by that asynchronous method if you are trying to change views or variables that are already in use within the main thread.

keep me posted and hope this helps.

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
0

This is why most apps haves a loading screen when fetching data asynchronously. It could be that there is nothing wrong with your code but the user is somehow able to tap the button before the data is fetched. You could try deploying the app on an emulator of a low performing phone. Check the model of the phone that the crashes are occurring

-1

@ig343

It's seems like your problem came because

Caused by com.******.******.users.UserData$DataNotFoundException: Requested element not found
   at com.******.******.users.UserData.getData(UserData.java:478)

Option 1: use exception handler {try catch to handle DataNotFoundException} for line (UserData.java:478) and log the error.

Option 2: Try to give all Requested element value as null.

Have fun....

Srinivasan M
  • 317
  • 1
  • 3
  • 11