使用alpha版本的材料组件在各种OS版本上崩溃的AppCompatDialogs

时间:2019-06-25 14:20:12

标签: android dialog material-components-android

首先,我知道Material Components 1.1.0仍处于alpha状态,并且我知道它可能不稳定,但是我对某些解决方案感兴趣。在我的代码中仍然可能有问题。

我正在使用自定义视图创建“警报对话框”,并且使用此类日志崩溃了。它可以在某些设备上完美运行,而在其他设备上则崩溃。

    android.content.res.Resources$NotFoundException: File res/drawable-v21/abc_dialog_material_background.xml from color state list resource ID #0x7f080019
    at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2750)
    at android.content.res.Resources.loadColorStateList(Resources.java:2699)
    at android.content.res.TypedArray.getColor(TypedArray.java:439)
    at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:3780)
    at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:3981)
    at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:1969)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:699)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:641)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:535)
    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:394)
    at android.app.Dialog.show(Dialog.java:295)
    at com.itemstudio.castro.services.dialogs.DialogsHelper.openScreenTesterSettingsDialog(DialogsHelper.kt:168)
    at com.itemstudio.castro.screens.tools.screen_tester_activity.ScreenTesterView$initClickListeners$1.onClick(ScreenTesterView.kt:25)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid color state list tag inset
    at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
    at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
    at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2746)
    at android.content.res.Resources.loadColorStateList(Resources.java:2699) 
    at android.content.res.TypedArray.getColor(TypedArray.java:439) 
    at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:3780) 
    at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:3981) 
    at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:1969) 
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:699) 
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:641) 
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:535) 
    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:394) 
    at android.app.Dialog.show(Dialog.java:295) 
    at com.itemstudio.castro.services.dialogs.DialogsHelper.openScreenTesterSettingsDialog(DialogsHelper.kt:168) 
    at com.itemstudio.castro.screens.tools.screen_tester_activity.ScreenTesterView$initClickListeners$1.onClick(ScreenTesterView.kt:25) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我的XML。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="24dp"
android:background="?attr/colorBackgroundSecondary"
android:paddingTop="24dp"
android:paddingEnd="24dp"
android:paddingBottom="8dp">

<TextView
    android:id="@+id/screenTesterOptionsLayoutTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:fontFamily="sans-serif-medium"
    android:text="@string/screen_tester_dialog_title"
    android:textColor="?android:attr/textColorPrimary"
    android:textSize="20sp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical">

        <Switch
            android:id="@+id/screenTesterOptionsDifferentColors"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="@string/screen_tester_dialog_different_colors" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:background="?attr/colorDivider" />

        <Switch
            android:id="@+id/screenTesterOptionsCoordinates"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="@string/screen_tester_dialog_coordinates" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:background="?attr/colorDivider" />

        <Switch
            android:id="@+id/screenTesterOptionsLines"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="@string/screen_tester_dialog_lines" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:background="?attr/colorDivider" />

        <Switch
            android:id="@+id/screenTesterOptionsVibration"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="@string/screen_tester_dialog_vibration" />

        <View
            android:id="@+id/screenTesterOptionsVibrationIntensityDivider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:background="?attr/colorDivider" />

        <TextView
            android:id="@+id/screenTesterOptionsVibrationIntensityTitle"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center_vertical"
            android:text="@string/screen_tester_dialog_vibration_intensity"
            android:textColor="?android:attr/textColorPrimary" />

        <RadioGroup
            android:id="@+id/screenTesterOptionsVibrationIntensitySelection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp">

            <RadioButton
                android:id="@+id/screenTesterOptionsVibrationIntensityLow"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="@string/screen_tester_dialog_vibration_intensity_low"
                android:textColor="?android:attr/textColorSecondary" />

            <RadioButton
                android:id="@+id/screenTesterOptionsVibrationIntensityMedium"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="@string/screen_tester_dialog_vibration_intensity_medium"
                android:textColor="?android:attr/textColorSecondary" />

            <RadioButton
                android:id="@+id/screenTesterOptionsVibrationIntensityHigh"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="@string/screen_tester_dialog_vibration_intensity_high"
                android:textColor="?android:attr/textColorSecondary" />

        </RadioGroup>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/screenTesterOptionsLayoutButtonApply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginTop="8dp"
            android:text="@string/screen_tester_dialog_action_apply"
            android:textColor="?attr/colorAccent" />

    </LinearLayout>
</ScrollView>

如果您认为这是框架中的错误,我将在Google的问题跟踪器中打开问题。

1 个答案:

答案 0 :(得分:0)

我现在发现的唯一解决方案是增强对话框主题叠加:

<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar" />
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>
</style>

<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="android:windowBackground">@color/white</item>
</style>