android.view.InflateException:二进制XML文件第2行:错误膨胀了类androidx.constraintlayout.widget.ConstraintLayout

时间:2019-08-02 10:18:50

标签: java android

我试图通过按钮和文本视图制作一个简单的文本更改应用程序,而自己尝试学习android开发,但是不幸的是,当我使用USB在自己的手机上运行它时,它开始显示该应用程序已经停止。如果您不介意帮助我理解y,那么此错误即将到来以及如何解决。

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" />

logcat

 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

1 个答案:

答案 0 :(得分:0)

这是在将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文件中的这两行。如果文件不存在,则创建它。 在迁移项目时。

现在,这差不多完成了

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

我认为它将对所有正在迁移项目的人都有效。