在我的项目中找不到 CoordinatorLayout

The CoordinatorLayout could not be found on my project

每次我使用更新的 androidx 库在 android studio 上创建一个涉及片段的新项目时,我都会得到 activity-main.xml 的空白预览阅读 android.coordinatorlayout...协调器布局。这使得我无法正确地看到预览,并且在某些情况下无法正确地与之交互,从而使项目工作变得困难。我刚刚创建了一个新的空白项目来展示发生了什么。存在以下渲染问题:

    java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at android.view.BridgeInflater.lambda$createViewFromCustomInflater[=10=](BridgeInflater.java:259)
    at android.view.BridgeInflater.createViewFromCustomInflater(BridgeInflater.java:285)
    at android.view.BridgeInflater.onCreateView(BridgeInflater.java:122)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:309)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:204)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1159)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1117)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:204)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1159)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1117)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:870)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834) 
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.google.android.material.theme.overlay.MaterialThemeOverlay
    at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:93)
    at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:88)
    at com.google.android.material.textview.MaterialTextView.<init>(MaterialTextView.java:83)
    at com.google.android.material.theme.MaterialComponentsViewInflater.createTextView(MaterialComponentsViewInflater.java:61)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:121)
    ... 39 more

有时会发生另一个渲染问题,有时会自行消失,因为我移动了不同的代码和设置,但也不确定是什么原因造成的:

android.view.InflateException: Class not found project
    at android.graphics.drawable.DrawableInflater_Delegate.inflateFromClass(DrawableInflater_Delegate.java:63)
    at android.graphics.drawable.DrawableInflater.inflateFromClass(DrawableInflater.java:201)
    at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:141)
    at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1402)
    at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1361)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1336)
    at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:360)
    at android.content.res.Resources_Delegate.getDrawable(Resources_Delegate.java:184)
    at android.content.res.Resources.getDrawable(Resources.java:922)
    at android.content.Context.getDrawable(Context.java:693)
    at androidx.core.content.ContextCompat$Api21Impl.getDrawable(ContextCompat.java:864)
    at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:483)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:148)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:136)
    at androidx.appcompat.widget.ResourceManagerInternal.checkVectorDrawableSetup(ResourceManagerInternal.java:493)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:141)
    at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:136)
    at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:66)
    at androidx.appcompat.widget.TintTypedArray.getDrawable(TintTypedArray.java:83)
    at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:318)
    at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:246)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:399)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:147)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:303)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1124)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:870)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: project not found.
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.findClass(LayoutlibCallbackImpl.java:899)
    at android.graphics.drawable.DrawableInflater_Delegate.inflateFromClass(DrawableInflater_Delegate.java:45)
    ... 51 more

此后的以下代码用于 activity_main.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/Theme.FixHelp.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/Theme.FixHelp.PopupOverlay" />

    </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_marginEnd="@dimen/fab_margin"
        android:layout_marginBottom="16dp"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我还将包括我的 build:gradle 以防问题与我在以下代码中的设置有关:

    plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.revolution.fixhelp"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

非常感谢任何帮助我已经尝试了几个小时来解决这个问题

尝试构建项目,如果失败则尝试使缓存失效。