我正在根据"Kotlin for Android Developers"一书中的说明测试一个基于Kotlin的基本Android应用程序.我使用的是Android Studio 2.1.1.
我有以下build.grade(Project:WeatherApp)设置:
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
我有以下build.grade(Module:App)设置:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
ext.support_version = '23.1.1'
ext.kotlin_version = '1.0.2'
ext.anko_version = '0.8.2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1" …
Run Code Online (Sandbox Code Playgroud) 我从"Kotlin In Action"MEAP书中得到以下代码,该书应该找到该组中人的最大年龄并返回该年龄段的所有人:
data class Person(val name: String, val age: Int)
val people = listOf(Person("Alice", 29), Person("Bob", 31))
people.filter {it.age == people.maxBy(Person::age)}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:"Kotlin:运算符'=='无法应用于'kotlin.Int'和'Person?'." 如何修复代码以使其工作?
我有一个基于从"生成操作"部分的代码下面的代码科特林这里:
val list = listOf(1,2,3,4,5,6)
val listRepeated = listOf(2,2,3,4,5,5,6)
println(list.merge(listRepeated) { it1, it2 -> it1 + it2 })
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Error:(33, 18) Kotlin: Unresolved reference: merge
Error:(33, 40) Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.
Error:(33, 45) Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.
Run Code Online (Sandbox Code Playgroud)
如何才能正确编译此代码?