250x250
반응형
04-28 23:59
Today
Total
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Notice
Recent Posts
Recent Comments
Link
Archives
관리 메뉴

Bill Kim's Life...

[Kotlin] java.lang.RuntimeException: Can't create handler inside thread Thread... that has not called Looper.prepare() 본문

CS(컴퓨터 과학)/Kotlin

[Kotlin] java.lang.RuntimeException: Can't create handler inside thread Thread... that has not called Looper.prepare()

billnjoyce 2023. 4. 29. 02:44
728x90
반응형

[이슈 내용]

 

Kotlin으로 앱 개발 중에 AlertDiglog 내지는 Toast 메시지를 출력 시 아래와 같은 쓰레드 이슈로 인하여 앱이 크래시가 나는 경우를 만날 수 있습니다.

java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-8,5,main] that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:227)
        at android.os.Handler.<init>(Handler.java:129)
        at android.app.Dialog.<init>(Dialog.java:179)
        at android.app.Dialog.<init>(Dialog.java:269)
        at com.joycestudios.common.Util$Companion.showAlert(Util.kt:203)

 

 

 

[해결 방법]

 

해당 이슈는 UI 작업을 Worker Thread에서 실행해야 하는데 Main Thread에서 실행할 경우 발생할 수 있습니다.

따라서 아래처럼 Handler 등을 통한 UI(Worker) Thread에서 실행함으로서 해당 이슈를 해결할 수 있습니다.

handler.post(new Runnable() {
    @Override
    public void run() {
        // 메시지 팝업 내지 토스트 메시지 출력
    }
});

 

 

 

그럼 즐거운 앱개발을 하시길 바랍니다.

 

 

감사합니다.

 

728x90
반응형
Comments