任何人都可以告訴我,爲什麼我要使用這個Kotlin類獲得java.lang.StackOverflowError
? 41號線是if (instance == null) {
StackOverflowError在Kotlin中使用Singleton
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 2.0工作室,科特林1.0.1-2
如果你想讓'TokenHelper'成爲一個singleton,只需將它聲明爲'object'並將'instance'屬性全部放在一起即可。編譯器會照顧你的樣板。 –
@KirillRakhman我相信你的評論是真正的答案。現在它是一個對象,我使用它像'TokenHelper.INSTANCE.getToken()'(在Java類中)。 – user3105453
我將其添加爲答案。 –