首页 > 解决方案 > java.lang.RuntimeException:无法启动活动 ComponentInfo,:子类 android.support.design.widget.AppBarLayout

问题描述

基本上我升级到androidx并且我有错误:

我试图在整个项目中更改 xml 文件以从新的 androidx 库中更新它们,但仍然不明白是什么问题。提前致谢

知道如何解决这个问题吗?

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.view.InflateException: Binary XML file line #11: Could not inflate Behavior subclass android.support.design.widget.AppBarLayout$ScrollingViewBehavior
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

这是我的 xml 文件:

activty_tracking_drawer

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_tracking_activity_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_tracking_activity_drawer"
        app:menu="@menu/activity_tracking_drawer_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

这是我的 app_bar_tracking_activity_drawer :

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout    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"
    tools:context=".Tracking.TrackingActivityDrawer">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >


            <ImageView
                android:id="@+id/your_icon"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:src="@drawable/maintoolbar" />


            <ImageView
                android:id="@+id/toolbarDogProfile"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:src="@drawable/beni" />



        </androidx.appcompat.widget.Toolbar>


    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_tracking_activity_drawer" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的毕业文件:

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])


implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.vectordrawable:vectordrawable:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

也检查过,开始另一个活动,但问题仍然存在,即使我将 xml 更改为 androidx,它仍然给我多个错误 Error inflating class

标签: android

解决方案


ComponentInfo 的布局 xml 文件仍然有一个条目,android.support.design.widget.AppBarLayout该条目是旧的支持库版本。

您需要将其更改为com.google.android.material.appbar.AppBarLayout新材料库的一部分。

每当你看到InflateException一个你所拥有的:

android.view.InflateException: Binary XML file line #11

这意味着框架由于问题而无法扩展您的 xml 文件。您可以看到它还为您提供了遇到问题的行号以及摘要:

无法膨胀行为子类 android.support.design.widget.AppBarLayout$ScrollingViewBehavior

在这种情况下,它告诉您您正在引用ScrollingViewBehaviour旧库中找不到的 a。您可能只需要更新对新库的引用,它就会正常工作。


推荐阅读