我无法弄清楚下面这段代码有什么问题。在我看来,getString必然会返回一个String。事实上,它适用于上面的行!这是一个错误吗?
fun loadExtraBundleIntoIntent(origIntent: Intent, resultIntent: Intent) {
origIntent.extras?.let {
if (it.getString("peerUid") != null) {
resultIntent.putExtra("fragment", "ChatFragment")
resultIntent.putExtra("peerUid", it.getString("peerUid"))
} else if (it.getString("fragment") != null) {
resultIntent.putExtra("fragment", it.getString("fragment"))
}
if (it.getString("action") != null)
resultIntent.putExtra("action", it.getString("action"))
}
}
错误
resultIntent.putExtra("fragment", it.getString("fragment"))
错误讯息:
Error:(80, 30) Overload resolution ambiguity:
public open fun putExtra(p0: String!, p1: Bundle!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Parcelable!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Serializable!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Array<(out) Parcelable!>!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Array<(out) CharSequence!>!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Array<(out) String!>!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Boolean): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: BooleanArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Byte): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: ByteArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Char): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: CharArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: CharSequence!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Double): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: DoubleArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Float): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: FloatArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Int): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: IntArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Long): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: LongArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: Short): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: ShortArray!): Intent! defined in android.content.Intent
public open fun putExtra(p0: String!, p1: String!): Intent! defined in android.content.Intent
答案 0 :(得分:4)
它认为从it.getString ("fragment")
返回的值为null,空值与所有这些putExtra
函数匹配。
例如:
resultIntent.putExtra("fragment", null)
Bundle,Parcelable,Serializable,Parcelable,CharSequence,String,BooleanArray,ByteArray,CharArray,CharSequence,DoubleArray,FloatArray,IntArray,LongArray,ShortArray,String
所有这些参数都是对象,null可以设置为所有这些参数 - &#34;过载分辨率模糊。所有这些功能都匹配&#34;
不知何故,编译器正在考虑'else if'
之后&#34;它&#34; 值可能为空,这不应该发生,因为它是Bundle!
非类型-空值。我相信这是一个编译器错误。
我已使用以下kotlin版本进行测试以编译您的代码:
然后,如果您使用版本 1.0.7 ,它将起作用! - 在Android工作室中设置此版本很困难。 或者您可以使用其他选项强制编译器避免/解决此问题,例如:
'!!'
:it!!.getString("fragment")
String
:it.getString("fragment") as String
?: ""
:it.getString(&#34; fragment&#34;)?:&#34;&#34; 答案 1 :(得分:1)
这看起来像编译器错误。但是,我建议你以一种只从Bundle中检索每个值的方式重写它,这样编译器就可以推断出putExtra()第二个参数总是非空的:
fun loadExtraBundleIntoIntent(origIntent: Intent, resultIntent: Intent) {
origIntent.extras?.let {
val peerUid = it.getString("peerUid")
if (peerUid != null) {
resultIntent.putExtra("fragment", "ChatFragment")
resultIntent.putExtra("peerUid", peerUid)
} else it.getString("fragment")?.let {
resultIntent.putExtra("fragment", it)
}
it.getString("action")?.let {
resultIntent.putExtra("action", it)
}
}
}
答案 2 :(得分:0)
愚蠢的错误:Kotlin没有if { } else if { }
声明!只有if { } else { }
。
编辑:我错了,有一个
否则如果
在Kotlin。
我不知道它为什么不编译。
答案 3 :(得分:0)
您正在使用相同的键值添加多个附加功能。这里你使用片段作为键值
resultIntent.putExtra("fragment", "ChatFragment")
再来一次
resultIntent.putExtra("fragment", it.getString("fragment"))
答案 4 :(得分:0)
应使用名词-空值填充值... 如果ur值为null,则无法使用它并出现此错误
喜欢:
intent.putExtra("PRICE", txt_price)
u应该传递正确的值LIKE:
intent.putExtra("PRICE", product.price)