此代码无法正常工作,并且该应用始终崩溃。每当我运行代码时,它就会显示错误。即使任何人都可以,请帮助我删除此错误。每当我在应用程序中使用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:致命异常:主要流程:com.example.weatherapp,PID:18681java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.weatherapp / com.example.weatherapp.MainActivity}:android.view.InflateException:二进制XML文件第21行:二进制XML文件行#21:膨胀类androidx.cardview.widget.CardView时出错在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)在android.app.ActivityThread.-wrap11(未知来源:0)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1616)在android.os.Handler.dispatchMessage(Handler.java:106)在android.os.Looper.loop(Looper.java:176)在android.app.ActivityThread.main(ActivityThread.java:6651)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)原因:android.view.InflateException:二进制XML文件第21行:二进制XML文件第21行:错误膨胀了类androidx.cardview.widget.CardView原因:android.view.InflateException:二进制XML文件行#21:错误膨胀了类androidx.cardview.widget.CardView引起原因:java.lang.reflect.InvocationTargetException在java.lang.reflect.Constructor.newInstance0(本机方法)在java.lang.reflect.Constructor.newInstance(Constructor.java:334)在android.view.LayoutInflater.createView(LayoutInflater.java:651)在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)在android.view.LayoutInflater.rInflate(LayoutInflater.java:867)在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)在android.view.LayoutInflater.rInflate(LayoutInflater.java:870)在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)在android.view.LayoutInflater.inflate(LayoutInflater.java:519)在android.view.LayoutInflater.inflate(LayoutInflater.java:427)在android.view.LayoutInflater.inflate(LayoutInflater.java:374)在androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)在androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)在com.example.weatherapp.MainActivity.onCreate(MainActivity.kt:48)在android.app.Activity.performCreate(Activity.java:7088)在android.app.Activity.performCreate(Activity.java:7079)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)在android.app.ActivityThread.-wrap11(未知来源:0)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1616)在android.os.Handler.dispatchMessage(Handler.java:106)在android.os.Looper.loop(Looper.java:176)在android.app.ActivityThread.main(ActivityThread.java:6651)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)造成原因:android.content.res.Resources $ NotFoundException:可绘制的android:color / secondary_text_light_nodisable,带有资源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.weatherappE / AndroidRuntime:位于android.view.View。(View.java:4784)在android.view.ViewGroup。(ViewGroup.java:597)在android.widget.FrameLayout。(FrameLayout.java:92)在android.widget.FrameLayout。(FrameLayout.java:87)在androidx.cardview.widget.CardView。(CardView.java:121)在androidx.cardview.widget.CardView。(CardView.java:117)在java.lang.reflect.Constructor.newInstance0(本机方法)在java.lang.reflect.Constructor.newInstance(Constructor.java:334)在android.view.LayoutInflater.createView(LayoutInflater.java:651)在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)在android.view.LayoutInflater.rInflate(LayoutInflater.java:867)在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)在android.view.LayoutInflater.rInflate(LayoutInflater.java:870)在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)在android.view.LayoutInflater.inflate(LayoutInflater.java:519)在android.view.LayoutInflater.inflate(LayoutInflater.java:427)在android.view.LayoutInflater.inflate(LayoutInflater.java:374)在androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)在androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)在com.example.weatherapp.MainActivity.onCreate(MainActivity.kt:48)在android.app.Activity.performCreate(Activity.java:7088)在android.app.Activity.performCreate(Activity.java:7079)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)在android.app.ActivityThread.-wrap11(未知来源:0)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1616)在android.os.Handler.dispatchMessage(Handler.java:106)在android.os.Looper.loop(Looper.java:176)在android.app.ActivityThread.main(ActivityThread.java:6651)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)引起原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件第18行:标记需要'drawable'属性或子标记定义一个可绘制在android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:189)在android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)在android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)在android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1295)在android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1254)在android.content.res.ResourcesImpl.createFromXmlForDensity(ResourcesImpl.java:1471)在android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:822)...另外39个**XML代码
<?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'