B3nedikt / restring

Restring is a android library to replace string resources dynamically
Apache License 2.0
317 stars 31 forks source link

Crash when opening AlertDialog #56

Closed edgar-zigis closed 4 years ago

edgar-zigis commented 4 years ago

It looks like that the other crashes are fixed, but there is one left. App crashes on some devices when opening AlertDialog. The exact crash:

Fatal Exception: android.content.res.Resources$NotFoundException: Resource ID #0x0
       at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:228)
       at android.content.res.Resources.loadXmlResourceParser(Resources.java:2170)
       at android.content.res.Resources.getLayout(Resources.java:1170)
       at dev.b3nedikt.restring.internal.RestringResources.getLayout(RestringResources.java:162)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
       at dev.b3nedikt.viewpump.internal.-ViewPumpLayoutInflater.inflate(-ViewPumpLayoutInflater.java:51)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
       at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
       at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
       at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
       at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
       at android.app.Dialog.dispatchOnCreate(Dialog.java:550)
       at android.app.Dialog.show(Dialog.java:391)
       at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)

Crashing devices: Samsung S8, S10e, A40 and Xiaomi Redmi 5A

edgar-zigis commented 4 years ago

Can't reproduce this though myself on my Samsung S7. This is how the actual piece of code looks like:

AlertDialog.Builder(context)
    .setTitle(context.getString(R.string.attention))
    .setMessage(R.string.some_message)
    .setCancelable(false)
     .setPositiveButton(context.getString(R.string.ok)) { dialog, _ ->
          dialog.cancel()
     }.show().apply {
         getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.blue))
     }
B3nedikt commented 4 years ago

@edgar-zigis I think the issue is in AlertController Line 191:

 mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);

The RestringResources don't return a valid resource id here, so 0 is used as default value, which leads to above crash. Not sure why though, I can't reproduce the issue on my Xiaomi Note 10, my Samsung S8 & my Nexus 5X.

Btw. are you using the framework AlertDialog or the one from appcompat?

edgar-zigis commented 4 years ago

Hey, it was androidx.appcompat.app.AlertDialog. I just have decided to replace it with just android.app.Dialog and now everything is fine.

B3nedikt commented 4 years ago

Think the actual reason for the crash is related to the Theme the context has when creating the AlertDialog. But this may be specific for your app, as I can't recreate it with my Samsung S8, so closed for now ;)