【问题标题】:Androidx error inflating viewAndroidx错误膨胀视图
【发布时间】:2018-06-09 12:07:19
【问题描述】:

我正在尝试使用 Androidx。该应用程序非常新,因此没有太多代码。我确实在 android studio 中使用了“Refactor to 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() 是你自己的代码吗?请发布实现。
  • 您是否尝试过重建应用程序?
  • 我已经尝试重建应用程序。 toast() 来自 Android KTX。 developer.android.com/kotlin/ktx

标签: android kotlin android-jetpack androidx android-ktx


【解决方案1】:

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

已编辑:作为Arturo Mejia's answer,只需按⇧⌘R或Ctrl + Shift + R即可

替换任何

androidx.constraintlayout.widget.ConstraintLayout

androidx.constraintlayout.ConstraintLayout

在所有使用约束布局的 XML 文件中。

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

旧的解决方法答案:

改变

implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

implementation 'androidx.constraintlayout:constraintlayout:1.1.0'

我尝试在 ConstraintLayout 中按 Command + Click androidx.constraintlayout.widget.ConstraintLayout 从 XML 文件到 显示原始类,但它没有找到任何东西。我编辑后 约束布局版本到 1.1.0 然后它就在那里。

【讨论】:

    【解决方案2】:

    在我的情况下更新依赖项

    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    

    有帮助

    【讨论】:

      【解决方案3】:

      我通过将 gradle 依赖项从 alpha3 更改为 alpha1 来修复它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多