首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用AppCompat/Material主题对类com.google.android.material.appbar.AppBarLayout进行错误充气

使用AppCompat/Material主题对类com.google.android.material.appbar.AppBarLayout进行错误充气
EN

Stack Overflow用户
提问于 2021-12-09 06:51:58
回答 2查看 683关注 0票数 1

我真的不知道我到底错过了什么,但我得到了相同的膨胀异常错误,即使是一个Appcomat主题。逻辑猫:

代码语言:javascript
运行
复制
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aasha/com.example.aasha.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3139)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
     Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout

布局:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chat_good_things">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/message_box">

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    app:layout_scrollFlags="scroll|enterAlways">

                    ....

                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/chat_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:clipToPadding="false"
                    app:layout_anchor="@id/toolbar"
                    app:layout_anchorGravity="bottom"
                    android:layout_gravity="bottom"/>
            </androidx.core.widget.NestedScrollView>

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <LinearLayout
            android:id="@+id/message_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" >

            <ImageButton
                android:id="@+id/image_send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@drawable/ic_image_add" />

            <EditText
                android:id="@+id/message_type_box"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:focusableInTouchMode="true"
                android:textIsSelectable="true"
                android:hint="Type a message..."
                android:inputType="text" />

            <ImageButton
                android:id="@+id/message_send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@drawable/ic_message_send"/>
        </LinearLayout>

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

主题:

代码语言:javascript
运行
复制
<style name="Theme.Aasha" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

格莱德尔:

代码语言:javascript
运行
复制
    implementation 'com.google.android.material:material:1.4.0'

    // androidx
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation "androidx.recyclerview:recyclerview:1.2.1"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation 'de.hdodenhof:circleimageview:3.1.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

我搜索并发现了这个StackOverflow,尝试了所有的解决方案,包括:

使缓存无效/重新启动,没有任何结果。Refactor -> 迁移到AndroidX,表示没有为该项目找到任何用途。

我感到困惑,做了一个单独的项目,与相同的单一布局运行,没有任何问题,但我无法区分和发现这里的错误。请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2021-12-09 07:15:21

不要忘记使用Theme.MaterialComponents.Light.NoActionBar (或任何真正的Theme.MaterialComponents.*.NoActionBar )作为父AppTheme在style.xml

代码语言:javascript
运行
复制
implementation 'com.google.android.material:material:1.0.0'

classpath 'com.android.tools.build:gradle:3.2.0'

将分级和材料设计改为最新版本

票数 1
EN

Stack Overflow用户

发布于 2021-12-09 15:19:16

我正在使用SplashScreenApi,这是来自androidx.core.splashscreen.SplashScreen库的。

我知道我必须在installSplashScreen() ()中的setContentView()之前调用setContentView(),但我不知道的是它应该在任何viewBinding初始化之前就被调用。对像我这样没有经验的人来说,这几天不知怎么没人注意到。该应用程序运行顺利,一切运行良好,直到任何材料设计标签的实现。

在此之前:

代码语言:javascript
运行
复制
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        installSplashScreen()
        setContentView(binding.root)
        ...
}

之后:

代码语言:javascript
运行
复制
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        installSplashScreen()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ...
}

我真是太蠢了。我花了一段时间,因为逻辑猫总是指向setContentView()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70285750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档