B3nedikt / ViewPump

View Inflation you can intercept.
Apache License 2.0
165 stars 15 forks source link

Error inflating class Button #10

Closed aurimas-zarskis closed 3 years ago

aurimas-zarskis commented 3 years ago

Hey,

after updating restring, viewpump and reword to newest versions I came across this crash on API 21 (newer api's seem to work just fine). It looks like there is some app theme mismatch that causes this crash.

Base theme we use is Theme.MaterialComponents.DayNight.NoActionBar, adding .Bridge to the theme fixes WebView issue, but then styles for buttons stop working on API 21.

After some digging around I found that adding <item name="viewInflaterClass">com.google.android.material.theme.MaterialComponentsViewInflater</item> to bridge theme would fix the styling, but WebView crash would come back.

I wonder if maybe it's related to https://github.com/B3nedikt/ViewPump/commit/b6e1f692564b1555e6e981b49f2e8c2994b929f7 and somehow WebView uses wrong theme to inflate the button?

appcompat - 1.2.0
material - 1.2.1
restring - 5.1.1
viewPump - 4.0.4
reword - 3.1.0
android.view.InflateException: Binary XML file line #103: Error inflating class Button
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
     at com.android.internal.app.AlertController.installContent(AlertController.java:233)
     at android.app.AlertDialog.onCreate(AlertDialog.java:356)
     at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
     at android.app.Dialog.show(Dialog.java:274)
     at com.android.org.chromium.content.browser.input.SelectPopupDialog.show(SelectPopupDialog.java:126)
     at com.android.org.chromium.content.browser.ContentViewCore.showSelectPopup(ContentViewCore.java:2467)
     at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
     at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:28)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5221)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
     at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
     at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
     at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
     at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
     at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:203)
     at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:194)
     at com.google.android.material.theme.MaterialComponentsViewInflater.createButton(MaterialComponentsViewInflater.java:43)
     at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:123)
     at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
     at androidx.appcompat.app.AppCompatDelegateWrapper.createView(AppCompatDelegateWrapper.kt:129)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate.access$createView$s1371502683(ViewPumpAppCompatDelegate.kt:28)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate$createView$1.invoke(ViewPumpAppCompatDelegate.kt:54)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate$createView$1.invoke(ViewPumpAppCompatDelegate.kt:28)
     at dev.b3nedikt.viewpump.internal.InterceptorChain.proceed(InterceptorChain.kt:26)
     at dev.b3nedikt.reword.RewordInterceptor.intercept(RewordInterceptor.kt:34)
     at dev.b3nedikt.viewpump.internal.InterceptorChain.proceed(InterceptorChain.kt:37)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate.inflate(ViewPumpAppCompatDelegate.kt:89)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate.createView(ViewPumpAppCompatDelegate.kt:47)
     at androidx.appcompat.app.ViewPumpAppCompatDelegate.onCreateView(ViewPumpAppCompatDelegate.kt:75)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
B3nedikt commented 3 years ago

Yeah, as the error says you need a theme based on Theme.MaterialComponents, which for whatever reason is missing here during inflation. I can't reproduce your issue though. Can you try forking restring and change the sample app so it reproduces the issue? Also does the issue appear only on real devices or also on an emulator? On a app of mine which uses a web view it seems to work on API 21 without issues at least on the emulator.

Edit: Have you tried setting the version to 4.0.3 and test it with API 21? If this has no issues the simplest fix would be to just use the fix on 4.0.4 only if API > 21

aurimas-zarskis commented 3 years ago

Hello again, after trying to reproduce this issue I found that it was caused by using Restring.locale = Locale("en"). What I did to reproduce this issue was change app theme to Theme.MaterialComponents.DayNight.NoActionBar and set restring locale using Restring.locale = Locale("en") in Application class and loaded html file with:

<select name="cars" id="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>

I am using API 21 emulator I have updated the sample app to simulate this: https://github.com/Aurimas-Zarskis/restring/tree/webview_crash

B3nedikt commented 3 years ago

@Aurimas-Zarskis Looks like indeed themes get stripped, I quick fixed this here: https://github.com/B3nedikt/restring/tree/webview_crash

Now the question remains why xD There is a way to solve this with the workaround in ViewPump more elegantly, I will release a new version in the next days.

B3nedikt commented 3 years ago

@Aurimas-Zarskis Now fixed in ViewPump 4.0.5!