Kotlin调用Java平台类型导致llegalStateException [英] Kotlin call Java platform types result in llegalStateException
问题描述
我在我的一个Android类中使用kotlin
,当试图从Bundle
获取更多内容时,似乎有时会弹出IllegalStateException
.
I'm using kotlin
in one of my Android classes and it seems like an IllegalStateException
sometimes pops up on this line, when trying to get an extra from a Bundle
.
keyOrTag = bundle.getString("tag")
val
这样声明
val keyOrTag: String
不幸的是,我没有完整的堆栈跟踪记录,因为我从GP控制台注意到了这一点.
Unfortunately I don't have the full stack trace as I've noticed this from the GP Console.
推荐答案
好的,我相信我知道为什么会这样.将其发布为答案,以便其他有相关问题的人可以看到.
Okay I believe I know why that happens. Posting it as an answer so that others with a related issue could see.
添加到捆绑软件中的String "tag"
实际上可以是将其发送到Kotlin
的Java类中的null
.而且由于我没有将val
声明为可为空,所以我相信这就是为什么它引发IllegalStateException(是的,kotlin中没有NPE)的原因.
The String "tag"
added to the bundle can actually be null
in the Java class that sends it over to the Kotlin
one. And since I didn't declare the val
as nullable I believe that's why it's throwing an IllegalStateException (yup, no NPE in kotlin).
解决方法:
val keyOrTag: String?
这篇关于Kotlin调用Java平台类型导致llegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!