akexorcist / Localization

[Android] In-app language changing library
Apache License 2.0
983 stars 154 forks source link

ClassCastException: LocalizationContext cannot be cast to android.app.ContextImpl on API 16 #65

Closed varrav closed 3 years ago

varrav commented 4 years ago

Hi! On android API 16 I get such exception (and crash):

java.lang.RuntimeException: Unable to start receiver com.xxx.InactivityHandler: java.lang.ClassCastException: com.akexorcist.localizationactivity.core.LocalizationContext cannot be cast to android.app.ContextImpl
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
        at android.app.ActivityThread.access$1500(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: com.akexorcist.localizationactivity.core.LocalizationContext cannot be cast to android.app.ContextImpl
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2226)
        at android.app.ActivityThread.access$1500(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method)

InactivityHandler is defined in AndroidManifest as

akexorcist commented 4 years ago

Does your app still support for API 16?

varrav commented 4 years ago

Does your app still support for API 16?

It's ~2% of traffic, while all main sdks used in my apps are support API 16 I will support it too.

I locally patched LocalizationUtility.kt: fun applyLocalizationContext for API 16 will return updated baseContext

val configBase = baseContext.resources.configuration
  configBase.locale = currentLocale
  baseContext.resources.updateConfiguration(configBase, baseContext.resources.displayMetrics)
  baseContext

instead of context which is LocalizationContext type

akexorcist commented 4 years ago

Apply this code only in API 16 or lower? Let me add this in the library

varrav commented 4 years ago

Apply this code only in API 16 or lower? Let me add this in the library

I tried only on API 16, as my lowest used API

akexorcist commented 4 years ago

Sorry, I forget to tell you that I fixed in 1.2.6. Please check it out.