javaandroidxmlandroid-constraintlayoutinflate-exception

Fatal Exception in Android Studio: Error inflating class ConstraintLayout


I'm getting the same error and cant seem to understand where it comes from. tried messing with the constraints but im keep coming back to this specific problem.

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rapp, PID: 11288
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rapp/com.example.rapp.ui.CheckListActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class ConstraintLayout
     Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class ConstraintLayout
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/base.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_resources_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.rapp.ui.CheckListActivity.onCreate(CheckListActivity.java:62)
E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
        Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.rapp-re4jZb52NhxBq505zHulTA==/split_lib_resources_apk.apk
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:353)
        at dalvik.system.DexFile.<init>(DexFile.java:100)
        at dalvik.system.DexFile.<init>(DexFile.java:74)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
        at dalvik.system.DexPathList.<init>(DexPathList.java:157)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:35)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:693)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:727)
        at android.app.LoadedApk.getResources(LoadedApk.java:954)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2270)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
                ... 6 more
Application terminated.

The XML file activity_check_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/constraintLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.CheckListActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/view_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

    </ScrollView>

</android.support.constraint.ConstraintLayout>




And the class that showed up in the error CheckListActivity.java:

package com.example.rapp.ui;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;

import com.example.rapp.R;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class CheckListActivity extends AppCompatActivity implements CheckListView {

    // states for buttons.
    public static final int ADD_NEW_ITEM = 1;
    public static final int REMOVE_LAST_ITEM = 2;
    public static final int GOT_FOCUS = 3;
    public static final int REMOVE_ITEM = 4;
    ArrayList<String> ingredientNames = new ArrayList<>(); // list of ingredients.
    private ConstraintLayout container;
    private CheckListPresenter presenter;
    private String jsonArrayStr;


    // method to read json from assets folder.
    public String loadJSONFromAsset(Context context) {
        String json = null;
        try {
            InputStream is = context.getAssets().open("foods.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                json = new String(buffer, StandardCharsets.UTF_8);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        System.out.println("success!");
        return json;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            jsonArrayStr = savedInstanceState.getString("json_array", "");
        }
//**error is happening here. **
        setContentView(R.layout.activity_check_list);
        container = findViewById(R.id.view_container);

        presenter = new CheckListPresenterImpl(this, getApplicationContext());

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("json_array", presenter.getCheckListData());
    }

    @Override
    public ConstraintLayout getContainer() {
        return container;
    }

    @Override
    public String getSavedData() {
        return jsonArrayStr;
    }

}

I tried adding compile 'com.android.support.constraint:constraint-layout:1.0.2' to the xml but with no success.

EDIT: here is the gradle.build file (tried constraint-layout 1.1.2):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.example.rapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Solution

  • used the answer given here: https://stackoverflow.com/a/53061877/5357676 unchecked "Enable Instant Run to hot swap code/resource changes on deploy" in Setting > Build, Execution, Deployment > Instant run