类型不匹配:推断类型是 String,但预期是 Int,Kotlin

问题描述 投票:0回答:2

任务

:app:compileDebugKotlin
失败

类型不匹配:推断类型为

String
,但预期为
Int

android android-studio kotlin admob
2个回答
1
投票

问题是什么? IDE 本身告诉您,需要一个 Int,但您正在传递一个 String。

要从资源中获取值,例如字符串、主题等...您需要传递资源的 Int 或 id,这是通过执行操作获得的

R.id.your_resource

0
投票

所以看看你的代码:

val InterstitialId = dataSnapshot.child("interstitial_id").value.toString()

当您调用

.toString
方法时,此行将返回一个字符串,下面 2 行将此变量传递给
.getString
,并且根据 documentation
getString()
需要一个 Int 而不是 String。

© www.soinside.com 2019 - 2024. All rights reserved.