任何人都可以说,为什么我使用这个Kotlin课程获得java.lang.StackOverflowError
?第41行是if (instance == null) {
class TokenHelper protected constructor() {
var token: String? = null
var appId: String? = null
var installationId: String? = null
var userId: String? = null
companion object {
var instance: TokenHelper? = null
get() {
if (instance == null) {
instance = TokenHelper()
}
return instance
}
}
}
堆栈跟踪:
04-11 19:07:42.188 16142-16142/com.foo.bar.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.foo.bar.debug, PID: 16142
java.lang.StackOverflowError: stack size 8MB
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:0)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41
04-11 19:07:42.271 16142-16142/com.foo.bar.debug D/Error: ERR: exClass=java.lang.StackOverflowError
04-11 19:07:42.271 16142-16142/com.foo.bar.debug D/Error: ERR: exMsg=stack size 8MB
04-11 19:07:42.271 16142-16142/com.foo.bar.debug D/Error: ERR: file=TokenHelper.kt
04-11 19:07:42.271 16142-16142/com.foo.bar.debug D/Error: ERR: class=com.foo.bar.helper.TokenHelper$Companion
04-11 19:07:42.271 16142-16142/com.foo.bar.debug D/Error: ERR: method=getInstance line=0
04-11 19:07:42.282 16142-16142/com.foo.bar.debug D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:0)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(TokenHelper.kt:41)
at com.foo.bar.helper.TokenHelper$Companion.getInstance(
04-11 19:07:42.283 16142-16142/com.foo.bar.debug D/Error: ERR: TOTAL BYTES WRITTEN: 880768
04-11 19:07:45.527 16142-16142/com.foo.bar.debug D/Process: killProcess, pid=16142
04-11 19:07:45.528 16142-16142/com.foo.bar.debug D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:113 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
Android Studio 2.0,Kotlin 1.0.1-2
答案 0 :(得分:8)
您以递归方式调用instance
getter方法。将您的getter定义更改为使用field
代替:
var instance: TokenHelper? = null
get() {
if (field == null) {
field = TokenHelper()
}
return field
}
文件中的相关摘录:
Kotlin中的类不能有字段。但是,有时它是 使用自定义访问器时必须具有支持字段。对于 为了这些目的,Kotlin提供了一个可以自动支持的领域 使用字段标识符访问:
var counter = 0 // the initializer value is written directly to the backing field set(value) { if (value >= 0) field = value }
字段标识符只能在属性的访问者中使用。
答案 1 :(得分:1)
改编自对原始答案的评论:
在Kotlin中,您不需要所有样板来声明单例。在宣布您的课程时,只需将其设为object TokenHelper {
var token: String? = null
var appId: String? = null
var installationId: String? = null
var userId: String? = null
}
即可:
TokenHelper.token
然后按如下方式访问它:TokenHelper.INSTANCE.getToken()
或来自Java {{1}}。