Androidx error inflating view
Asked Answered
L

3

6

I'm trying to use Androidx. The app is pretty new, so there is not much code. I did use the "Refactor to Androidx" option in android studio. But sometime after that, it stopped working. I don't know what made it stop working.

What am I supposed to do?

But it get this error

Error inflating class androidx.constraintlayout.widget.ConstraintLayout

Main activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        pick_image.setOnClickListener {
            toast("Pick image clicked")
        }

    }
}

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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/pick_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Pick image"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle (app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.alvarlagerlof.blurr"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Androidx
    implementation 'androidx.core:core-ktx:1.0.0-alpha3'
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.1'


    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
Lighthouse answered 9/6, 2018 at 12:7 Comment(3)
Is toast() your own code? Please post the implementation.Antemundane
Did you tried rebuilding the app?Anabaptist
I have tried rebuilding the app. toast() is from Android KTX. developer.android.com/kotlin/ktxLighthouse
T
5

EDITED 2: Since the newer version, they've reverted back to androidx.constraintlayout.widget.ConstraintLayout. Just keep reading if you're using constraint layout version 1.1.1

EDITED: As Arturo Mejia's answer, just press ⇧⌘R or Ctrl + Shift + R to

replace any

androidx.constraintlayout.widget.ConstraintLayout

with

androidx.constraintlayout.ConstraintLayout

in all XML file that use Constraint Layout.

That's a change since constraint layout version v1.1.1 (in v1.1.0 ConstraintLayout class is still inside ".widget" package)

Old workaround answer:

Change from

implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

to

implementation 'androidx.constraintlayout:constraintlayout:1.1.0'

I tried to press Command + Click at ConstraintLayout in androidx.constraintlayout.widget.ConstraintLayout from XML file to show the original class but it doesn't find any thing. After I edited the constraint layout version to 1.1.0 then it's there.

Tautonym answered 9/6, 2018 at 15:46 Comment(0)
M
2

In my case updating dependency

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

to

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

has helped

Mucilage answered 22/12, 2018 at 11:3 Comment(0)
L
0

I fixed it by changing the gradle dependencies form alpha3 to alpha1

Lighthouse answered 9/6, 2018 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.