【问题标题】:App Crashing with android material Chip Group with material 1.3.0应用程序崩溃与 android 材料芯片组与材料 1.3.0
【发布时间】:2021-02-13 07:56:45
【问题描述】:

我对此有疑问

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.truesight/com.truesight.MainActivity}: android.view.InflateException: Binary XML file line #27 in com.truesight:layout/activity_main: Binary XML file line #27 in com.truesight:layout/activity_main: Error inflating class com.google.android.material.chip.Chip
 Caused by: android.view.InflateException: Binary XML file line #27 in com.truesight:layout/activity_main: Binary XML file line #27 in com.truesight:layout/activity_main: Error inflating class com.google.android.material.chip.Chip
     Caused by: android.view.InflateException: Binary XML file line #27 in com.truesight:layout/activity_main: Error inflating class com.google.android.material.chip.Chip
 Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

这是我的 xml 代码,我正在尝试在相对布局和滚动视图中使用带有水平滚动视图的芯片组

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@drawable/grad_bg"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <com.google.android.material.chip.ChipGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:singleLine="true"
            app:singleSelection="true">

            <com.google.android.material.chip.Chip
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/et_bg"
                android:padding="12dp"
                android:text="Valorant"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <com.google.android.material.chip.Chip
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/et_bg"
                android:padding="12dp"
                android:text="CS:GO"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <com.google.android.material.chip.Chip
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/et_bg"
                android:padding="12dp"
                android:text="DotA 2"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <com.google.android.material.chip.Chip
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/et_bg"
                android:padding="12dp"
                android:text="PUBG"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <com.google.android.material.chip.Chip
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/et_bg"
                android:padding="12dp"
                android:text="Mobile Legends"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp" />


        </com.google.android.material.chip.ChipGroup>


    </HorizontalScrollView>


<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottom_nav"
    app:itemBackground="@color/nav_color"
    app:itemTextColor="@drawable/selector"
    app:itemIconTint="@drawable/selector"
    app:menu="@menu/menu_nav"
    android:layout_alignParentBottom="true"/>


</RelativeLayout>

我被设置为我的父母主题

<style name="AppTheme." parent="Theme.MaterialComponents.DayNight.NoActionBar">

并使用依赖项

implementation 'com.google.android.material:material:1.3.0'

【问题讨论】:

  • 为什么要在主题名称后添加一个点AppTheme.

标签: java android android-layout android-chips


【解决方案1】:

尝试将theme 单独添加到您的所有chips 中,这将覆盖应用程序或父主题

<com.google.android.material.chip.Chip
    <!-- other props -->
    android:theme="@style/Theme.MaterialComponents.Light />

【讨论】:

    【解决方案2】:

    用这个替换你的 AppTheme:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
    

    【讨论】:

    • Bridge 更好~ "Bridge 主题继承自 AppCompat 主题,同时也为你定义了新的 Material Components 主题属性。如果你使用的是 bridge 主题,你可以在不改变你的应用的情况下开始使用 Material Design 组件主题。”
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多