gpt4 book ai didi

java - android.view.InflateException : Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout

转载 作者:行者123 更新时间:2023-12-02 09:38:20 29 4
gpt4 key购买 nike

我正在尝试自己学习android开发,因为我试图使用按钮和 TextView 制作一个简单的文本更改应用程序,但是当使用USB在我自己的手机上运行它时,不幸的是,它开始显示该应用程序已经停止。如果您不介意帮助我了解此错误的发生以及如何解决它。

MainActivity.java

 package com.nanb.wishes;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView txt1, txt2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView txt1 = (TextView) findViewById(R.id.txt1);
final TextView txt2 = (TextView) findViewById(R.id.txt2);
Button btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt1.setText("Happy birthday to a special person who is bringing so much joy with her smile. I am thankful for every moment we spend together, and I wish your happiness never ends.");
txt2.setText("");
}
});

btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
txt1.setText(" May this day be as sunny as your smile, and as beautiful as you are. You shine every day, but on this day you will shine the brightest. Happy Birthday");
txt2.setText("");
return true;
}
});
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/birthday"
tools:context=".MainActivity">

<TextView
android:id="@+id/txt1"
android:layout_width="328dp"
android:layout_height="113dp"
android:layout_marginStart="38dp"
android:layout_marginTop="203dp"
android:layout_marginEnd="61dp"
android:layout_marginBottom="464dp"
android:fontFamily="@font/aclonica"
android:text="@string/welcome"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txt2"
android:layout_width="325dp"
android:layout_height="107dp"
android:layout_marginStart="38dp"
android:layout_marginEnd="61dp"
android:layout_marginBottom="332dp"
android:fontFamily="@font/aclonica"
android:text="@string/step"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt1" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="142dp"
android:layout_marginTop="83dp"
android:layout_marginEnd="168dp"
android:layout_marginBottom="201dp"
android:background="@drawable/button"
android:padding="10dp"
android:text="Click Here"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt2" />

日志猫

 08-02 15:34:04.717 31123-31123/com.nanb.wishes E/AndroidRuntime: FATAL 
EXCEPTION: main
Process: com.nanb.wishes, PID: 31123
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanb.wishes/com.nanb.wishes.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.nanb.wishes.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5268) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.nanb.wishes.MainActivity.onCreate(MainActivity.java:20) 
at android.app.Activity.performCreate(Activity.java:6033) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5268) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
Caused by: android.content.res.Resources$NotFoundException: Resource "com.nanb.wishes:drawable/birthday" (7f070055) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f070055 a=-1 r=0x7f070055}
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2456)
at android.content.res.Resources.loadDrawable(Resources.java:2396)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.<init>(View.java:3748)
at android.view.ViewGroup.<init>(ViewGroup.java:498)
at android.view.ViewGroup.<init>(ViewGroup.java:494)
at android.view.ViewGroup.<init>(ViewGroup.java:490)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:575)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.nanb.wishes.MainActivity.onCreate(MainActivity.java:20) 
at android.app.Activity.performCreate(Activity.java:6033) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
at `enter code here`android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5268) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
08-02 15:39:05.031 31123-31123/? I/Process: Sending signal. PID: 31123 SIG: 9

最佳答案

这是将android项目迁移到androidX项目后会出现的一些错误。

以下是检查或解决这些错误的一些步骤

第 1 步:- 确保您在 gradle 文件中的实现。

第 2 步:- Android studio 自动更改所有库的实现及其根据 android X 的使用。如果不知何故未完成,则在 .xml 文件中更改

示例:-

     androidx.constraintlayout.ConstraintLayout (wrong).

更改为

     androidx.constraintlayout.widget.ConstraintLayout (right).

注意:-您必须添加

     android.enableJetifier=true
android.useAndroidX=true

这两行位于 gradle.properties 文件中。如果该文件不存在,则创建它。当您迁移项目时。

现在,这几乎完成了

如果之后还出现错误,请构建 > 重建。 您可以从文件菜单中选择“无效缓存/重新启动”。

我认为这对所有正在迁移项目的人都会有效且有帮助。

关于java - android.view.InflateException : Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com