-1

I've just set up a new blank project for a simple calculator app with the intent of learning and applying ViewModel, LiveData and DataBinding as well as testing a math expression parser I've developed learning Kotlin. I've created the viewmodel class, enabled data binding and imported the necessary dependencies for viewmodel and livedata. But when I try to run the code just for testing it gives me this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calcapp, PID: 31418
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calcapp/com.example.calcapp.CalculatorActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class layout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5459)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class layout
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
    at com.example.calcapp.CalculatorActivity.onCreate(CalculatorActivity.kt:9)
    at android.app.Activity.performCreate(Activity.java:6259)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class layout
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) 
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) 
    at com.example.calcapp.CalculatorActivity.onCreate(CalculatorActivity.kt:9) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.layout" on path: DexPathList[[zip file "/data/app/com.example.calcapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.calcapp-2/lib/arm, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.view.LayoutInflater.createView(LayoutInflater.java:583)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:677)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696) 
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170) 
    at com.example.calcapp.CalculatorActivity.onCreate(CalculatorActivity.kt:9) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    Suppressed: java.lang.ClassNotFoundException: android.view.layout
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 25 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Apparently it has something to do with the data tag I'm trying to set up on the XML file, but I couldn't figure out what's wrong - I've done everything exactly like in the documentation and tutorials.

Here's my XML:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="viewModel"
            type="com.example.calcapp.CalculatorViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CalculatorActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

The Main Activity and ViewModel are just blank:

package com.example.calcapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class CalculatorActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_calculator)
    }
}
package com.example.calcapp

import androidx.lifecycle.ViewModel

class CalculatorViewModel : ViewModel() {

}

And here's my grade.app file:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.calcapp"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    // Expression Parser and Calculator
    implementation 'com.github.jfransp:ExpressionParserUtil-Android-Library:802333a7d3'
    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
jfransp
  • 85
  • 7
  • Take a look at this question while it's not 100% the same, [accepted answer](https://stackoverflow.com/a/51579759/1905965) might help you – Amin May 01 '21 at 15:24
  • I've tried everything from this answer but I'm still getting the same issue. When I put my cursor on top of the data tag it says "Element data is not allowed here", but I don't know why. I've wrapped everything in the tag as you can see on the original post, data binding is enable on the build.gradle file, and my binding classes are generated. – jfransp May 02 '21 at 03:18
  • I've solved it. The guy on the tutorial I was following put the data tag inside the constraintlayout and I didn't watch enough of the video to see him realizing his mistake, so I did the same thing and couldn't figure out what was wrong! So dumb :( But thank you anyway! – jfransp May 02 '21 at 03:34

1 Answers1

1

You may need to enable dataBinding in your build.gradle file

android {
    ...
    dataBinding {
        enabled true
    }
}

Source: https://developer.android.com/codelabs/android-databinding#2

Eric.1
  • 71
  • 3