56

I have an app uploaded on PlayStore and getting crash on it java.lang.NullPointerException from android.app.ActivityThread.updateLocaleListFromAppContext

I have tried searching a lot online to check where this error could be coming from. I'm not natively using this method call specifically for anything in the code.

java.lang.NullPointerException: 
  at android.app.ActivityThread.updateLocaleListFromAppContext (ActivityThread.java:5892)
  at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6127)
  at android.app.ActivityThread.access$1200 (ActivityThread.java:240)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1797)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7076)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:965)

Please help if anyone knows the solution or where I can start looking to resolve this.

To clarify, this is not an issue of how to avoid a common NullPointerException, this does not happen in the developers code but rather on the side of Android API itself and the question is on how to avoid it, as it cannot be resolved directly. The issue still exists.

NoHarmDan
  • 492
  • 5
  • 16
Richie
  • 593
  • 1
  • 5
  • 9
  • 1
    In crash log u can find the java class and line number – Piyush Jun 17 '19 at 07:06
  • I have pasted the crash log i obtained from the playstore in my question. The error is in default android classes. – Richie Jun 17 '19 at 07:07
  • 1
    I found something xda developers site https://forum.xda-developers.com/showpost.php?p=72836226&postcount=581 – VinothKumar Subramani Jul 18 '19 at 04:29
  • No solution on that forum link as well @VinothKumarSubramani – Richie Jul 29 '19 at 13:25
  • 4
    2 years later, I'm also seeing that issue on Android 9, and sadly no indication on its root cause. Any progress on finding a solution? – 3c71 Sep 09 '19 at 06:57
  • 9
    Same here. For me the issue started appearing abruptly 1 week ago. Unable to detect where it's coming from. It only happens on OnePlus devices. – saberrider Sep 16 '19 at 07:56
  • 1
    I'm getting this error heavily on Samsung J series devices running Android 9. – Richie Nov 08 '19 at 05:02
  • 1
    and still there.. and only on OnePlus – aleksandrbel May 29 '20 at 18:12
  • 1
    https://stackoverflow.com/q/60629355/13600988 Found the same error that's resolved by converting bitmap to byte array before passing in intent. It May be that you too are trying to pass bitmap directly with intent. – Shubham Agrawal Jul 18 '20 at 22:15
  • 1
    @ShubhamAgrawal I didn't pass any bitmap with intent but I'm still getting this error from Firebase Crashlytics, I don't think it is related to bitmap. I got this error on Vsmart Joy 3 running Android10. (Unfortunately, I don't own a Vsmart and I can't reproduce the error) – Lynch Chen Apr 16 '21 at 09:55
  • still there... so far only seen on OnePlus5, Android 9 – John Pang Jun 02 '21 at 13:25
  • same here...this NPE just appeared for me on a "UMIDIGI Bison GT" using Android 10. Judging from stacktrace, i can only guess that it has something to do with localization... Happened for just one user on said device. – nils277 Apr 25 '22 at 12:22
  • the method that updatelocale one is called to Decides whether to update an Activity's configuration and whether to inform it. so probably has to do with either config change or inntent where activity are reloading – Akash Pal Jul 10 '23 at 17:58
  • You need to check all variables and methods. Also, if you are using third-party SDKs, then you must make sure that their methods cannot return null, or that you handle them correctly. You can also use this [service](https://www.browserstack.com/) as an emulator. It seems there are versions of smartphones and which this error occurs. – Ivan Abakumov Jul 30 '23 at 10:13

2 Answers2

0

Upon checking the question comments and in which devices this issue occurs, it appears it is specific to a subset of devices, such as OnePlus devices, Samsung J series. The common part is for them to be Android 9 devices. I've examined the source code for Android 9 (here is the method call which is at line 5750 and here is the method itself which is at line 5520) and I can say this code is not related to configuration changes. While the links' line numbers are not exactly the same, they are related in distance (e.g. 5750 - 5520 = 280 which is from source code, and 6127 - 5892 = 285 which is from the stack trace. Quite close but not exact) so I am assuming the framework is calling that part. The method, ActivityThread.handleBindApplication is called at first launch, and configuration changes happen only with activity recreations, the app's configuration does not change. At least, the given method is not called again.

I can also say that the method updateLocaleListFromAppContext was successfully called and the code crashed inside the method, not at the start.

Here is the relevant part of the code:

/**
 * The LocaleList set for the app's resources may have been shuffled so that the preferred
 * Locale is at position 0. We must find the index of this preferred Locale in the
 * original LocaleList.
 */
private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
    final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
    final int newLocaleListSize = newLocaleList.size();
    for (int i = 0; i < newLocaleListSize; i++) {
        if (bestLocale.equals(newLocaleList.get(i))) {
            LocaleList.setDefault(newLocaleList, i);
            return;
        }
    }

    // The app may have overridden the LocaleList with its own Locale
    // (not present in the available list). Push the chosen Locale
    // to the front of the list.
    LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
}

Now, there are a couple things to consider,

  • This is taken from AOSP (Open source project) and device manufacturers usually modify these core classes. The method inside the devices may have been modified, but it is a low possibility.
  • Google Play Console can sometimes trim out the information about the stack trace.
  • If the stack trace is exact, this method can only crash at newLocaleList.size() and bestLocale.equals(newLocaleList.get(i)) calls. The reason of that is: when the stack traces are generated, their exception always contains the last code, and other lines would've left another stack trace.
  • Another possibility is: because of the for loop and the usage of newLocaleListSize, the newLocaleList variable's size could've been changed while the code was iterating, causing the crash at newLocaleList.get(i). This one is a bit more unlikely though, since the get(i) method would've been visible on crash, assuming the data is complete.

Based on the info, the most likely culprits are the variables bestLocale and newLocaleList in the method. Upon examining the source code more, the newLocaleList variable which is passed, is created via AppBindData which is passed down via a handler, and its information is updated in handleBindApplication. The method calls specific information update using Android's PackageManager, which is used to query information about the APK / loaded app, such as its info, manifest data, metadata, activities etc...

This information is usually delivered from system to the app using IPC (or AIDL) and that information can sometimes get corrupt, causing this error. Thus, it may be possible that newLocaleList variable is null.

Since this is a problem in the system, to possibly avoid it I can only suggest a couple things:

  • Analyze the devices themselves if possible and see if the app crashes in debug build,
  • Ensure your strings are completely converted in all languages and you do not rely on default language (a.k.a primary language that has all the strings but the specific configurations do not have them, unless they are marked with translatable=false in the strings.xml file)
  • Check if a similar report exists in Firebase Crashlytics if it is used (it may not be there since the app is crashing before the app is properly created but Firebase services can still upload sometimes. It can also contain more info about the stack trace.)
  • Check if the crash occurs in Android 9 Emulator (the official emulator and a Genymotion emulator)
  • You can also use an online samsung device from Samsung Test Lab (albeit for a short time) and see if the crash is observable. You might get a complete stack trace and it could point you to the right direction.

These are my findings, hopefully they are useful in some way.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
-1

This can be coz of some modules you are using which is not compatible with some (set of)devices. If the NullPointerException is occurring within the Android API itself and not in your app's code, it indicates a potential issue with device configuration. As a developer, there are certain steps you can take to mitigate the problem and try to avoid the crash:

  1. Update the App: Ensure that your app is using the latest version of Android SDK and libraries. If there are any known issues or bugs related to the NullPointerException in older versions, they might have been fixed in the newer releases.

  2. Check for Device Compatibility: Sometimes, certain devices or Android versions might be more prone to this issue. You can check the crash logs on the Play Store Developer Console to see if there's a pattern related to specific devices or Android versions. If the crash is prevalent on a particular device or Android version, you might need to test your app on that device or emulator to reproduce the issue and identify possible workarounds.

  3. Use Try-Catch Blocks: While you mentioned that the NullPointerException is happening in the Android API and not in your code, you can still try using try-catch blocks around certain critical parts of your code. This might not directly solve the issue but could help your app gracefully handle the crash and provide a better user experience. For example:

try {
    // Code that may cause NullPointerException
} catch (NullPointerException e) {
    // Handle the exception gracefully
}
  1. Monitor Updates and Release Notes: Keep an eye on the Android API updates and release notes. Google frequently releases new versions of the Android system and SDKs to address bugs and security issues. By staying updated, you can benefit from bug fixes and improvements that might indirectly resolve the NullPointerException.

  2. Implement a Crash Reporting Tool: Consider implementing a crash reporting tool in your app, such as Firebase Crashlytics or Bugsnag. These tools can help you monitor crashes and exceptions in real-time and provide insights into the root cause. Even if you can't directly fix the issue, having crash reports can help you understand the scope and impact of the problem.

Remember that some crashes might be specific to certain device configurations, and it may not always be possible to completely eliminate them. The above steps are intended to help you mitigate the impact of such crashes and provide a more stable experience to the majority of your users.

sarath
  • 343
  • 8
  • 31