[英]Type mismatch. Required Map<String, Any> Found Map<String, Any?>
I am trying to execute select using anko
extension in kotlin
like below 我试图用执行选择anko
扩展在kotlin
像下面
fun read() {
database.use {
select(PersonTable.Name).exec {
select("myTable").exec() {
parseList(
object : MapRowParser<Map<String, Any>> {
override fun parseRow(columns: Map<String, Any?>): Map<String, Any> {
Log.d("Hello", "Hello")
return columns;
}
}
)
}
}
}
}
I am getting an error on return column
我在return column
上遇到错误
Type mismatch. Required Map<String, Any> Found Map<String, Any?>
If I change to override fun parseRow(columns: Map<String, Any>): Map<String, Any>
then it shows an error. 如果我更改以override fun parseRow(columns: Map<String, Any>): Map<String, Any>
则显示错误。
build.gradle build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.williams.fourthdemo"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile "org.jetbrains.anko:anko-common:0.10.1"
compile "org.jetbrains.anko:anko-sqlite:0.10.1"
}
Is this bug in the anko library ? 这是anko库中的错误吗?
columns
has type Map<String, Any?>
, note that Any?
columns
类型为Map<String, Any?>
,请注意, Any?
is whatever object or null
. 是任何对象或null
。 But method's return type is Map<String, Any>
, Any
can't be null. 但是方法的返回类型为Map<String, Any>
, Any
不能为null。
For example, you may filter incoming map, leaving only non-null values: columns.filterValues { it != null } as Map<String, Any>
. 例如,您可以过滤传入的映射,仅保留非null值: columns.filterValues { it != null } as Map<String, Any>
。 It contains an unchecked cast, but it's safe. 它包含未经检查的演员表,但很安全。
Looks like 看起来像
object : MapRowParser<Map<String, Any>> {
should really be 应该真的是
object : MapRowParser<Map<String, Any?>> {
It is very common for databases to have columns that return a null in the cell. 数据库具有在单元格中返回null的列是很常见的。 The way you defined it as Any
means null is never going to happen. 您将其定义为Any
方式永远不会发生null。 If you cast your return to a Map<String, Any>
then you will simply end up with runtime errors happening if a null is contained anywhere. 如果将返回值转换为Map<String, Any>
那么如果在任何地方包含null,则只会导致运行时错误。
You are best suited by making it an Any?
您最适合将其设为“ Any?
type which forces the parseRow return type to be Map<String, Any?>
as well and fixes the potential bug in your code that would happen if a null value were to creep up somewhere. 类型,该类型也将强制parseRow返回类型也设置为Map<String, Any?>
并修复代码中可能会出现的错误(如果将null值爬到某个地方)。
It's not a bug in Anko. 这不是Anko中的错误。 It's a bug in your code. 这是您代码中的错误。
This function is wrong: 该函数是错误的:
override fun parseRow(columns: Map<String, Any?>): Map<String, Any> {
Log.d("Hello", "Hello")
return columns;
}
You are taking an input and returning it as-is but with a different type. 您正在接受输入并按原样返回它,但输入类型不同。
Your input is of type Map<String, Any?>
but you have to somehow transform it into Map<String, Any>
输入的类型为Map<String, Any?>
但是您必须以某种方式将其转换为Map<String, Any>
It would be as if you defined a function that takes String and returns Int like this: 就像您定义了一个接受String并返回Int这样的函数一样:
fun parseString(s: String): Int {
return s
}
This will obviously not compile. 这显然不会编译。
The interface you're trying to implement is basically: 您尝试实现的接口基本上是:
interface MapRowParser<out T> {
fun parseRow(columns: Map<String, Any?>): T
}
You're supposed to provide a function that converts a dictionary representing a database row, to some concrete object of your choosing. 您应该提供一个函数,将代表数据库行的字典转换为您选择的某些具体对象。 But you have to actually provide code to do the processing. 但是您必须实际提供代码来进行处理。
Alternatively, Anko provides you a function that can automagically match a table row to a class constructor, which is what I would prefer to use, instead of doing a manual mapping of dictionary keys to class fields. 另外,Anko为您提供了一个函数,该函数可以自动将表行与类构造函数匹配,这是我更喜欢使用的函数,而不是手动将字典键映射到类字段。
data class User(val userId: Int, val userName: String)
val rowParser = classParser<User>()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.