在android中夸大类android.support.design.widget.CollapsingToolbarLayout的错误

当筵意气临九霄,星离雨散不终朝。这篇文章主要讲述在android中夸大类android.support.design.widget.CollapsingToolbarLayout的错误相关的知识,希望能为你提供帮助。
我尝试使用CollapsingToolbarLayout向我的android应用程序添加视差视图。但是该应用崩溃了,日志是

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gbs.lms/com.gbs.lms.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class android.support.design.widget.CollapsingToolbarLayout at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$800(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5293) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class android.support.design.widget.CollapsingToolbarLayout at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) at com.gbs.lms.MainActivity.onCreate(MainActivity.java:31) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$800(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5293) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.CollapsingToolbarLayout" on path: DexPathList[[zip file "/data/app/com.gbs.lms-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at android.view.LayoutInflater.createView(LayoutInflater.java:571) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) at com.gbs.lms.MainActivity.onCreate(MainActivity.java:31) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$800(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5293) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Suppressed: java.lang.ClassNotFoundException: android.support.design.widget.CollapsingToolbarLayout at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

【在android中夸大类android.support.design.widget.CollapsingToolbarLayout的错误】app_bar_home。 xml
< ?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=".MainActivity"> < com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> < android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:expandedTitleGravity="top" app:toolbarId="@+id/toolbar"> < ImageView android:id="@+id/header" android:layout_width="match_parent" android:layout_height="match_parent" android:src="https://www.songbingjia.com/android/@mipmap/ic_launcher" android:fitsSystemWindows="true" android:scaleType="centerCrop" app:layout_collapseMode="parallax" /> < TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@android:color/white" android:layout_marginLeft="32dp" android:layout_marginStart="32dp" android:layout_marginBottom="32dp" android:layout_marginEnd="32dp" app:layout_collapseMode="parallax" android:layout_gravity="bottom" android:text="Like LuaPass? Buy me a coffee :)"/> < android.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" /> < /android.support.design.widget.CollapsingToolbarLayout> < /com.google.android.material.appbar.AppBarLayout> < include layout="@layout/content_main" /> < com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> < /androidx.coordinatorlayout.widget.CoordinatorLayout>

我也使用Parallax滚动尝试过它,但是它可以正常工作,但是我无法将其更改为所需的模型。我想在滚动时在应用栏中设置用户名。对于任何肯定的回答,谢谢。
答案
原因:java.lang.ClassNotFoundException:找不到类“ android.support.design.widget.CollapsingToolbarLayout”
由于您使用的是androidx和Material Components Library,因此必须在同一库中使用the component。使用:
< com.google.android.material.appbar.CollapsingToolbarLayout ..>

而不是android.support.design.widget.CollapsingToolbarLayout

    推荐阅读