Androidx错误:解析视图时出错

6

我正在尝试使用Androidx。这个应用程序非常新,因此代码不多。我确实在Android Studio中使用了“重构为Androidx”的选项。但是在那之后的某个时候,它停止工作了。我不知道是什么原因导致它停止工作。

我该怎么办?

但是我收到了这个错误信息。

Error inflating class androidx.constraintlayout.widget.ConstraintLayout

主要活动

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 (应用程序)

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'
}

toast() 是你自己的代码吗?请发布实现。 - Enzokie
你尝试重新构建应用程序了吗? - jonathanrz
我已经尝试重新构建应用程序。toast()是来自Android KTX的。https://developer.android.com/kotlin/ktx - alvarlagerlof
3个回答

5

编辑2: 自新版本以来,他们已经恢复使用androidx.constraintlayout.widget.ConstraintLayout。 如果您使用的是约束布局版本1.1.1,请继续阅读。

编辑: 根据Arturo Mejia的答案,只需按⇧⌘R或Ctrl + Shift + R,即可替换在所有使用约束布局的XML文件中使用的任何androidx.constraintlayout.widget.ConstraintLayoutandroidx.constraintlayout.ConstraintLayout

这是自约束布局版本v1.1.1以来的变化(在v1.1.0中,ConstraintLayout类仍位于“.widget”包内)

旧的解决方法是:

implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

改为

implementation 'androidx.constraintlayout:constraintlayout:1.1.0'

我试图在XML文件中从 androidx.constraintlayout.widget.ConstraintLayout 按Command + Click查找原始类,但它没有找到任何内容。在我将约束布局版本编辑为1.1.0之后,它就出现了。


2
在我的情况下,更新依赖项。
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

为了

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

已经帮助


0
我通过将 Gradle 依赖从 alpha3 更改为 alpha1 来解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接