任何人都可以解释为什么这个 android 布局不起作用并且 activity 一启动就崩溃 activity

Can anyone please explain why is this android layout not working and crashes the activity as soon as the activity is launched

此代码无效,应用程序一直崩溃。每当我 运行 代码时,它都会显示错误。即使在任何人都可以帮助我删除此错误之后。 每当我在我的应用程序中使用 TextInputLayout 时,也会发生此错误。

Gradle :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.weatherapp"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
    implementation 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

//    implementation 'com.google.android.gms.play-services-location:17.0.0'
    implementation 'com.google.android.libraries.places:places:2.2.0'
    implementation 'com.karumi:dexter:6.1.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

错误日志

**

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.weatherapp, PID: 18681 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapp/com.example.weatherapp.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.cardview.widget.CardView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) Caused by: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.cardview.widget.CardView Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class androidx.cardview.widget.CardView Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.view.LayoutInflater.createView(LayoutInflater.java:651) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.rInflate(LayoutInflater.java:870) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) at com.example.weatherapp.MainActivity.onCreate(MainActivity.kt:48) at android.app.Activity.performCreate(Activity.java:7088) at android.app.Activity.performCreate(Activity.java:7079) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) Caused by: android.content.res.Resources$NotFoundException: Drawable android:color/secondary_text_light_nodisable with resource ID

 Caused by: android.content.res.Resources$NotFoundException: File res/color/secondary_text_light_nodisable.xml from drawable resource ID

    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:837)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:638)
    at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:316)
    at android.content.res.Resources.loadDrawable(Resources.java:900)
    at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:930)

2020-04-21 19:19:16.157 18681-18681/com.example.weatherapp E/AndroidRuntime: at android.view.View.(View.java:4784) at android.view.ViewGroup.(ViewGroup.java:597) at android.widget.FrameLayout.(FrameLayout.java:92) at android.widget.FrameLayout.(FrameLayout.java:87) at androidx.cardview.widget.CardView.(CardView.java:121) at androidx.cardview.widget.CardView.(CardView.java:117) at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.view.LayoutInflater.createView(LayoutInflater.java:651) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.rInflate(LayoutInflater.java:870) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) at com.example.weatherapp.MainActivity.onCreate(MainActivity.kt:48) at android.app.Activity.performCreate(Activity.java:7088) at android.app.Activity.performCreate(Activity.java:7079) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: tag requires a 'drawable' attribute or child tag defining a drawable at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:189) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122) at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142) at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1295) at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1254) at android.content.res.ResourcesImpl.createFromXmlForDensity(ResourcesImpl.java:1471) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:822) ... 39 more ** XML code

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:background="#B0EEF6"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:padding="20dp">



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">

            <androidx.cardview.widget.CardView
                app:cardElevation="5dp"
                app:cardCornerRadius="10dp"
                android:background="@android:color/secondary_text_light_nodisable"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="20dp">

                    <ImageView
                        android:layout_width="50dp"
                        android:id="@+id/iv_main"
                        android:layout_height="50dp"
                        android:src="@drawable/snowflake"/>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="end"
                        android:layout_weight="2"
                        android:orientation="vertical"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Weather"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textSize="17sp"
                            android:textColor="@android:color/black"
                            android:id="@+id/tv_weather"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="condition"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textColor="@color/quantum_grey600"
                            android:textSize="15sp"
                            android:id="@+id/tv_condition"/>


                    </LinearLayout>

                </LinearLayout>

            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                app:cardElevation="5dp"
                android:layout_marginStart="10dp"
                app:cardCornerRadius="10dp"
                android:background="@android:color/secondary_text_light_nodisable"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="20dp">

                    <ImageView
                        android:layout_width="50dp"
                        android:id="@+id/iv_humid"
                        android:layout_height="50dp"
                        android:src="@drawable/humidity"/>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="end"
                        android:layout_weight="2"
                        android:orientation="vertical"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Degree"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textSize="17sp"
                            android:textColor="@android:color/black"
                            android:id="@+id/tv_degree"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="per cent"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textColor="@color/quantum_grey600"
                            android:textSize="15sp"
                            android:id="@+id/tv_percent"/>


                    </LinearLayout>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">

            <androidx.cardview.widget.CardView
                app:cardElevation="5dp"
                app:cardCornerRadius="10dp"
                android:background="@android:color/secondary_text_light_nodisable"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="20dp">

                    <ImageView
                        android:layout_width="50dp"
                        android:id="@+id/iv_temp"
                        android:layout_height="50dp"
                        android:src="@drawable/temperature"/>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="end"
                        android:layout_weight="2"
                        android:orientation="vertical"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Minimum"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textSize="17sp"
                            android:textColor="@android:color/black"
                            android:id="@+id/tv_minimum"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Maximum"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textColor="@color/quantum_grey600"
                            android:textSize="15sp"
                            android:id="@+id/tv_maximum"/>


                    </LinearLayout>

                </LinearLayout>

            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                app:cardElevation="5dp"
                android:layout_marginStart="10dp"
                app:cardCornerRadius="10dp"
                android:background="@android:color/secondary_text_light_nodisable"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="20dp">

                    <ImageView
                        android:layout_width="50dp"
                        android:id="@+id/iv_wind"
                        android:layout_height="50dp"
                        android:src="@drawable/wind"/>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="end"
                        android:layout_weight="2"
                        android:orientation="vertical"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Wind"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textSize="17sp"
                            android:textColor="@android:color/black"
                            android:id="@+id/tv_wind"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="miles/hour"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textColor="@color/quantum_grey600"
                            android:textSize="15sp"
                            android:id="@+id/tv_miles"/>


                    </LinearLayout>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

        </LinearLayout>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="wrap_content"
            app:cardElevation="5dp"
            app:cardCornerRadius="10dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:src="@drawable/location"
                        android:id="@+id/iv_location"/>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="end"
                        android:layout_weight="2"
                        android:orientation="vertical"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Name"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textSize="17sp"
                            android:textColor="@android:color/black"
                            android:id="@+id/tv_name"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="country"
                            android:gravity="end"
                            android:textStyle="bold"
                            android:textColor="@color/quantum_grey600"
                            android:textSize="15sp"
                            android:id="@+id/tv_country"/>


                    </LinearLayout>


                </LinearLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_margin="16dp"
                    android:background="@color/quantum_grey" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:src="@drawable/sunrise"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="sunrise"
                            android:gravity="center"
                            android:id="@+id/tv_sunrise"/>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_marginStart="20dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:src="@drawable/sunset"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="sunset"
                            android:gravity="center"
                            android:id="@+id/tv_sunset"/>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

    </LinearLayout>

请添加卡片视图依赖

implementation 'androidx.cardview:cardview:1.0.0'

并使用

app:background="@android:color/secondary_text_light_nodisable"

app:cardBackgroundColor="@android:color/secondary_text_light_nodisable"