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

I am trying to learn the android development by my self as I was trying to make a simple text changing app using a button and a text view but while running it on my own mobile using USB it started showing, unfortunately, the app has stopped. if you don’t mind in helping me to understand y this error is coming and how to resolve it.

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
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

This is some errors which will come after the migration android project to androidX project. Here is some step to check or solve those errors Step 1 :- Make Sure about your implementation in gradle file. Step 2 :- Android studio automatically change all libraries implementation and it’s uses according to android X. if somehow not done then change in .xml file Example :- androidx.constraintlayout.ConstraintLayout (wrong). change to androidx.constraintlayout.widget.ConstraintLayout (right). Note:- You have to add android.enableJetifier=true android.useAndroidX=true this two-line in gradle.properties file. If the file doesn’t exist then create it. While you are migrating your Project. Now, This is almost… Read more »