Build Error : Attribute "android:layout_width" was already specified for element "androidx.fragment.app.FragmentContainerView"

Build Error : Attribute "android:layout_width" was already specified for element "androidx.fragment.app.FragmentContainerView"

这是我在尝试构建我的应用程序时遇到的构建输出错误。我只在我的应用程序中使用过一次“FragmentContainerView”,它没有在该文件中显示任何错误。

这是 xml 代码 -

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_view"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:defaultNavHost="true"
        app:navGraph="@navigation/app_navigation" />
</layout>

如您所见,布局参数只定义了一次。我还添加了 gradle 文件,以防万一...

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.kenetic.blockchainvs"
        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
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures { viewBinding true }
    dataBinding { enabled true }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //jetpack
    def nav_version = "2.4.1"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    //room
    def room_version = "2.4.2"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

    //data-store
    def data_store_version = "1.0.0"
    implementation "androidx.datastore:datastore-preferences:$data_store_version"

    //life-cycle
    def lifecycle_version = "2.4.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
}

这里的逻辑是我已经用过

android:fitsSystemWindows="true"

所以,这些已经是我的布局尺寸限制。这意味着我不再需要

android:layout_width="match_parent"
android:layout_height="match_parent"

主要activity。所以,只需将其删除。这适用于您的根布局。因为我的根布局是 activity main(里面有我所有的片段),所以我从那里删除了约束。