无法解析符号 Theme.MaterialComponents.Light.NoActionBar(Android Studio)

9
我在进行多次Gradle更新后,在`styles.xml`文件中遇到了一个错误,该错误为`'Theme.MaterialComponents.Light.NoActionBar'`(由于错误而被标记为红色)。此外,我的组件排列也与我的设计想法不符。
我已经尝试过清理并重新构建项目、无效的缓存/重启、更新Gradle(可能不是正确的答案),但都没有解决问题。也许这只是我作为初学者的问题,因此我想可能只有我遇到了这个问题:')。
以下是`styles.xml`文件的内容:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/hitam</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

项目Gradle
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.4.2'
    classpath 'com.google.gms:google-services:4.2.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

应用 Gradle
      apply plugin: 'com.android.application'
      apply plugin: 'com.google.gms.google-services'

      android {
          compileSdkVersion 29
          defaultConfig {
          applicationId "blablabla"
          minSdkVersion 23
          targetSdkVersion 29
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner               "androidx.test.runner.AndroidJUnitRunner"

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '29.0.1'
}

compileOptions {

    sourceCompatibility JavaVersion.VERSION_1_6
    targetCompatibility JavaVersion.VERSION_1_6

    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.10.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.firebase:firebase-core:17.0.1'
    implementation 'com.google.firebase:firebase-auth:18.1.0'
    implementation 'com.google.firebase:firebase-database:18.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
}

apply plugin: 'com.google.gms.google-services'
buildToolsVersion = '29.0.1'

          }

我认为当地球上没有任何人遇到这个错误时,它是不可见的。求救 T_T


你好 "kindkind",欢迎来到 StackOverflow!请考虑提供一个最小可复现示例,以便我们能够帮助您解决问题。 - Edric
另外,为什么要将Java的目标和源兼容性设置为多个版本? - Edric
我已经忘记为什么写那段代码了,但它解决了我的其他编程问题,一旦解决了,我就认为它是成功的哈哈。 - kindkind
3个回答

24

Material Components主题已包含在Material Components for Android库中。

由于您正在使用androidx库,只需将此依赖项添加到您的app/build.gradle文件中即可。

dependencies {
    // ...
    implementation 'com.google.android.material:material:x.x.x'
    // ...
  }

目前(2022年4月29日),最新的稳定版本是

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

在这里,您可以找到所有有关设置的信息。


嗨@kindkind,如果这个或任何答案解决了您的问题,请考虑通过点击复选标记接受它。这向更广泛的社区表明您已找到解决方案,并为回答者和您自己赢得了一些声誉。没有义务这样做。 - Gabriele Mariotti
哦,没错,我删除它是因为我不知道我需要它,谢谢。 - Ronen Festinger
我的解决方法是将该实现升级到最新版本。 - Dave Hubbard

0
在 /android/app/build.gradle 中添加 Android Material 的依赖:
dependencies {
    // ...
    implementation 'com.google.android.material:material:<version>'
    // ...
}

要查找最新版本,请访问Google Maven

在/android/app/src/main/res/values/styles.xml中设置主题:

//<style name="LaunchTheme" parent="Theme.AppCompat"> - remove
<style name="LaunchTheme" parent="Theme.MaterialComponents.NoActionBar"> - add

0
MaterialComponents主题未显示的原因是您没有在app/build.gradle文件中包含所需的Material Components库。
dependencies {
    implementation 'com.google.android.material:material:<version>'
}

当前可用版本已在项目的 GitHub 发布页面中列出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接