尝试使用Anko verticalLayout时出现EOFException

时间:2019-08-08 09:25:50

标签: android kotlin anko

tutorial之后,我通过在app模块中添加build.gradle来导入Anko库:

implementation "org.jetbrains.anko:anko-sdk25:0.10.5"
implementation "org.jetbrains.anko:anko-appcompat-v7:0.10.5"

我尝试示例:

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

class StartActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {

        verticalLayout {
            editText {
                hint = "Enter your name"
            }
            button("ECHO"){
                setOnClickListener {
                    toast("Button clicked")
                }
            }
        }
    }
}

由于某种原因,`import org.jetbrains.anko。*变得很出色,并且AndroidStudio通过将verticalLayout标记为红色来显示verticalLayout错误。当我将鼠标悬停在上面时,会收到错误消息:

java.io.EOFException
    at java.io.DataInputStream.readInt(DataInputStream.java:392)
    at org.jetbrains.kotlin.metadata.builtins.BuiltInsBinaryVersion$Companion.readFrom(BuiltInsBinaryVersion.kt:29)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment.readProto(MetadataPackageFragmentProvider.kt:136)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment.computeMemberScope(MetadataPackageFragmentProvider.kt:105)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment.access$computeMemberScope(MetadataPackageFragmentProvider.kt:72)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment$memberScope$1.invoke(MetadataPackageFragmentProvider.kt:96)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment$memberScope$1.invoke(MetadataPackageFragmentProvider.kt:72)
    at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:346)
    at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:402)
    at org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment.getMemberScope(MetadataPackageFragmentProvider.kt:128)
    at org.jetbrains.kotlin.descriptors.impl.LazyPackageViewDescriptorImpl$memberScope$1.invoke(LazyPackageViewDescriptorImpl.kt:46)
    at org.jetbrains.kotlin.descriptors.impl.LazyPackageViewDescriptorImpl$memberScope$1.invoke(LazyPackageViewDescriptorImpl.kt:30)
    at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:346)
    at org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:402)
    at org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter.getWorkerScope(LazyScopeAdapter.kt:23)
    at org.jetbrains.kotlin.resolve.scopes.AbstractScopeAdapter.getContributedClassifier(AbstractScopeAdapter.kt:44)
    at org.jetbrains.kotlin.descriptors.FindClassInModuleKt.fin...

我该怎么办,这样就不会再出现此错误了?

1 个答案:

答案 0 :(得分:1)

尝试将导入内容更改为以下内容:

import org.jetbrains.anko.button
import org.jetbrains.anko.editText
import org.jetbrains.anko.toast
import org.jetbrains.anko.verticalLayout

这对我来说很好:enter image description here

,它也可以编译并运行:(剪掉一些工具栏,因为其中有项目名称)

enter image description here

这是我的依赖项列表,这是一个全新的项目,因此所有内容都是默认的:

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

即使将其更改为您发布的内容,也对我有效:

enter image description here

可能与您的Kotlin版本或android studio错误有关,请尝试更新您的IDE,因为我正在使用此默认项目

Gradle: 3.4.0
Kotlin: 1.3.31

和此版本的android studio(希望有帮助):

Android Studio 3.4
Build #AI-183.5429.30.34.5452501, built on April 10, 2019
JRE: 1.8.0_152-release-1343-b01 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6

旁注:我意识到OP没有setContentView,所以我想我也必须尝试一下,

enter image description here enter image description here