使用Anko布局预览获取IDE错误

时间:2019-02-13 11:15:26

标签: anko anko-component

更新:这可能与以下事实有关:我使用的是最新的androidx.support库,而不是旧的android.support。将朝那个方向进行调查并再次更新。 更新2:即使在创建使用旧版android.support.*库的项目之后,仍然会出现相同的错误。最后粘贴应用build.gradle

打开Anko Layout Preview工具窗口时,出现以下IDE错误。我的玩具项目的完整代码在堆栈跟踪下面:

java.lang.IllegalArgumentException: java.lang.ClassCastException@5d0792b9
    at sun.reflect.GeneratedConstructorAccessor1138.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.kotlin.android.dslpreview.DslPreviewClassResolver$Companion.createTypeMapper(DslPreviewClassResolver.kt:131)
    at org.jetbrains.kotlin.android.dslpreview.DslPreviewClassResolver$Companion.access$createTypeMapper(DslPreviewClassResolver.kt:122)
    at org.jetbrains.kotlin.android.dslpreview.DslPreviewClassResolver.resolveClassDescription(DslPreviewClassResolver.kt:116)
    at org.jetbrains.kotlin.android.dslpreview.DslPreviewClassResolver.getOnCursorPreviewClassDescription(DslPreviewClassResolver.kt:59)
    at org.jetbrains.kotlin.android.dslpreview.AnkoNlPreviewManager.refresh(AnkoNlPreviewManager.kt:122)
    at org.jetbrains.kotlin.android.dslpreview.AnkoNlPreviewManager.getBoundXmlFile(AnkoNlPreviewManager.kt:81)
    at com.android.tools.idea.uibuilder.editor.NlPreviewForm.getFile(NlPreviewForm.java:220)
    at com.android.tools.idea.uibuilder.editor.NlPreviewForm.initNeleModelWhenSmart(NlPreviewForm.java:422)
    at com.android.tools.idea.uibuilder.editor.NlPreviewForm.lambda$initNeleModel$5(NlPreviewForm.java:418)
    at com.intellij.openapi.project.DumbServiceImpl.lambda$smartInvokeLater$7(DumbServiceImpl.java:438)
    at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:315)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.doRun(LaterInvocator.java:447)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:431)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:415)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
    at java.awt.EventQueue.access$500(EventQueue.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:715)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:817)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:758)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:394)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

以下是我唯一的.kt文件的代码:

package com.foobar.deleteme

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.*

class MainActivityUI : AnkoComponent < MainActivity > {
    override fun createView(ui: AnkoContext<MainActivity>) = ui.apply {
        verticalLayout {
            button("bar")
        }
    }.view
}

class MainActivity : AppCompatActivity() {

    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        MainActivityUI().setContentView(this)
    }
}

我在主项目中遇到了同样的错误,因此我创建了一个只有一个活动的小型玩具项目。我又遇到了同样的错误。

app build.gradle:(ext.kotlin_version = '1.3.21'ext.anko_version = '0.10.8'

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.softrithms.deleteme"
        minSdkVersion 26
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation "com.android.support.constraint:constraint-layout:1.1.0-beta4"
    implementation "org.jetbrains.anko:anko:$anko_version"
    implementation "org.jetbrains.anko:anko-constraint-layout:$anko_version"
}

0 个答案:

没有答案