Search code examples
androidandroid-constraintlayoutinflate-exception

Error inflating class androidx.constraintlayout.widget.ConstraintLayout in Android 4


My app crashed when inflate the layout in Android 4.4, the error is

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.splashscreen.SplashScreen}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout

I have used androidx.constraintlayout:constraintlayout:2.0.0-beta3 or androidx.constraintlayout:constraintlayout:1.1.3 and it still crashed. It crashed on android < 5 only, and working fine on android >= 5

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splashscreen);
    }
<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/containers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_gradient_primary"
    tools:context=".ui.splashscreen.SplashScreen">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/bg_splash"
        app:tint="?attr/colorPrimaryDark" />

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo_izzy_white" />

</androidx.constraintlayout.widget.ConstraintLayout>

It is weird because it crashed only on Android < 5

Edited

I found that crashed caused by drawable. I have try to use support library

android {
   defaultConfig {
      vectorDrawables.useSupportLibrary = true
   }
}

and

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

in application class, but it still crash. This is my drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="315"
        android:centerColor="?attr/theme_primary"
        android:endColor="?attr/theme_primaryMiddle"
        android:startColor="?attr/theme_primaryLight" />
</shape>

Solution

  • I found my crash is because the gradient, on pre lollipop device it makes crash so I create 2 drawable. The first one is solid color only on drawable folder, and gradient on drawable-v24.