首页 > 解决方案 > 调用 createView 失败,如何修复 xml 中的 android 屏幕预览无法在 intellij 中正确显示

问题描述

我对 android 相当陌生,一旦我创建了一个新的 Android 项目并更新了所有依赖项,我就开始面临 xml 预览的设计选项卡中未正确呈现的问题。有一个错误说Call to createView failed。我在下面附上了一张快照。

快照intellij-idea

这是我的build.gradle文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "io.github.animeshz.airbent_android"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    android.sourceSets {
        androidTest.java.srcDirs += "src/androidTest/kotlin"
//        debug.java.srcDirs += "src/debug/kotlin"
        main.java.srcDirs += "src/main/kotlin"
        test.java.srcDirs += "src/test/kotlin"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.61"

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
    implementation "io.ktor:ktor-client-android:$ktor_version"
}

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

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

在xml编辑器中弹出的错误详细信息下:

java.lang.AssertionError: Call to createView failed
    at android.view.BridgeInflater.lambda$createViewFromCustomInflater$0(BridgeInflater.java:269)
    at android.view.BridgeInflater.createViewFromCustomInflater(BridgeInflater.java:286)
    at android.view.BridgeInflater.onCreateView(BridgeInflater.java:124)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:310)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:323)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:394)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:200)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:572)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:698)
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1700)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

任何帮助,将不胜感激!

标签: androidxmlandroid-layoutintellij-idea

解决方案


因此 Intellij IDE 建议更新的最新版本的依赖项存在问题。我恢复到这四个依赖项的旧版本:

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

并在下面打开Show Layout Decorations附加快照:

在此处输入图像描述

这解决了这个问题。如果他/她遇到同样的问题,希望其他人会发现这很有用!


推荐阅读