Android Studio上的Kotlin生成错误<&书信电报;intent.putExtra(";string";,it.getString(";string";)>;

Android Studio上的Kotlin生成错误<&书信电报;intent.putExtra(";string";,it.getString(";string";)>;,android,android-studio,kotlin,Android,Android Studio,Kotlin,我不知道下面的代码有什么问题。在我看来getString必然返回一个字符串。事实上,它在上面的代码行中工作!这是一个bug吗 fun loadExtraBundleIntoIntent(origIntent: Intent, resultIntent: Intent) { origIntent.extras?.let { if (it.getString("peerUid") != null) { resultIntent.putExtra("fr

我不知道下面的代码有什么问题。在我看来getString必然返回一个字符串。事实上,它在上面的代码行中工作!这是一个bug吗

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
错误:(80,30)过载分辨率模糊:
public open fun putExtra(p0:String!,p1:Bundle!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Parcelable!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Serializable!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Array!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Array!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Array!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Boolean):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:BooleanArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Byte):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:ByteArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Char):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:CharArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:CharSequence!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Double):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:DoubleArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Float):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:FloatArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Int):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:IntArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Long):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:LongArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:Short):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:ShortArray!):Intent!在android.content.Intent中定义
public open fun putExtra(p0:String!,p1:String!):Intent!在android.content.Intent中定义

它认为从
It.getString(“fragment”)
返回的值为null,并且null值与所有这些
putExtra
函数匹配

e、 g:
resultent.putExtra(“片段”,null)

Bundle,Parcelable,Serializable,Parcelable,CharSequence,String,BooleanArray,ByteArray,CharArray,CharSequence,DoubleArray,FloatArray,IntArray,LongArray,ShortArray,String

所有这些参数都是对象,可以将null设置为所有这些参数-“重载解析模糊度。所有这些函数都匹配”

不知何故,编译器认为“it”值可以在
“else if”
之后为null,这不应该发生,因为它是一个类型
包!
非null。我相信这是一个编译器错误

我已使用以下kotlin版本进行了测试,以编译您的代码:

  • 1.1.3(于2017年6月23日发布):kotlin构建错误
  • 1.1.2(于2017年4月25日发布):kotlin构建错误
  • 1.0.7(2017年3月15日发布):编译!无错误
然后,如果您使用版本1.0.7,它将工作!-在Android studio中设置此版本很困难。 也可以使用其他选项强制编译器避免/修复此问题,例如:

  • “!!”
    it!!.getString(“片段”)
  • 转换为
    String
    it.getString(“片段”)作为String
  • ?:“”
    :it.getString(“片段”):“”
  • 等等

您正在放置多个具有相同键值的附加值。这里您使用片段作为键值

 resultIntent.putExtra("fragment", "ChatFragment")
还有这里

resultIntent.putExtra("fragment", it.getString("fragment"))

这看起来像是一个编译器错误。但是,我建议您重写它,使您只从Bundle中检索每个值一次,这样编译器就可以推断putExtra()第二个参数始终为非null:

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)
        }
    }
}

应使用名词null值填充该值。。。 如果ur值为null,则无法使用它并接受此错误

比如:

 intent.putExtra("PRICE", txt_price)
u应传递正确的值,如:

  intent.putExtra("PRICE", product.price)

谢谢,“it”为null对于错误消息来说非常有意义。但是我判断“it”不可能为null。事实上,请看下面我自己的回答:问题是Kotlin中没有“else-if”,这很奇怪,但它就是这样,因此出现了错误:一个模糊的语法错误。我错了,有一个“else-if”在Kotlin.So中,我仍然不知道错误的原因。在我看来,这像是一个编译器错误。我不认为“it.getString(“string”)”可以为null。可以吗?如果可以,为什么在第一个“If”块中没有错误触发,而在“else If”块中触发block?谢谢你用不同的Kotlin版本进行测试,Victor。事实上,这肯定是一个编译器错误。我会在Kotlin的问题跟踪程序上填写的错误报告中报告你的发现。在修复之前,我只会显式地(冗余地)报告你的发现按照您的建议,使用“!!”运算符将其设置为非空类型。很好地向Kotlin团队报告了此问题。那么,这个问题是否得到了回答?或者您是否需要更多信息?抱歉,答案已被回答。这是有意的。因为它是“if{}else if{}else if{}”,无法在运行时同时运行这两行代码-这是一行还是另一行。请参阅