티스토리 뷰
IT/Android
[Android] Only the original thread that created a view hierarchy can touch its views. | android.view.ViewRootImpl$CalledFromWrongThreadException:
ttoogi 2023. 2. 21. 14:27
RecordActivity
운동기록을 담당하는 화면. 시간이 카운트업되는 타이머 기능이 필요.
D/EGL_emulation: app_time_stats: avg=42.88ms min=1.75ms max=592.13ms count=20
D/OpenGLRenderer: endAllActiveAnimators on 0xb400007bbd626a10 (RippleDrawable) with handle 0xb400007acd662cb0
E/MainActivity: onStop Called
E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: com.example.mystrava, PID: 19872
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:9758)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1959)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.view.View.requestLayout(View.java:26281)
at android.widget.TextView.checkForRelayout(TextView.java:10091)
at android.widget.TextView.setText(TextView.java:6633)
at android.widget.TextView.setText(TextView.java:6454)
at android.widget.TextView.setText(TextView.java:6406)
at com.example.mystrava.RecordActivity$onCreate$$inlined$timer$default$1.run(Timer.kt:156)
at java.util.TimerThread.mainLoop(Timer.java:563)
at java.util.TimerThread.run(Timer.java:513)
타이머 화면을 만들고 싶었는데, 해당 화면만 전환되면 종료됨.
참고
https://devfarming.tistory.com/3
원인
original thread가 아닌 이외의 thread에서 화면을 변경할 경우 에러가 발생한다고 함.
해결
timer thread 에서 text를 소요 시간으로 변경해서 출력하고자 한 코드가 잘못된 것 같아서 apply {} 에 text 셋팅 부분을 주석처리 했더니 안 죽고 정상 수행됨.
class RecordActivity : AppCompatActivity() {
private var time = 0
private var timerTask: Timer? = null
override fun onCreate(savedInstanceState: Bundle?) {
(...)
timer (period = 1000, initialDelay = 1000) {
second++
print(second)
if (second == 10) {
cancel()
Log.w("RecordActivity", "Timer Stop!")
}
timer.apply {
// 아래를 주석으로 막음
// text = second.toString()
Log.w("RecordActivity", "second: $second")
}
}
}
https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
Activity | Android Developers
developer.android.com
'IT > Android' 카테고리의 다른 글
TODO:: targetSdk 로그에서 확인하기 (0) | 2023.03.08 |
---|---|
[Android] TextView VS AppCompatTextView (0) | 2023.02.21 |
[Android] NPE Error | Unable to instantiate activity ComponentInfo | java.lang.NullPointerException (0) | 2023.02.21 |
[KOTLIN] 연산자 우선순위 (operator precedence) (0) | 2023.02.17 |
Intent로 다른 Activity로 데이터 전달하면서 화면 전환 (0) | 2023.02.10 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 프로그래머스
- Repository pattern
- 내채공
- Kotlin
- VLLO
- 클라우드 네이티브 애플리케이션 디자인 패턴
- 코틀린
- https://kotlinlang.org/docs/object-declarations.html
- 안드로이드
- 디자인패턴
- gitlab_ssh_permission_denied
- 맥북개발환경
- 통신패턴
- 아이폰영상편집어플후기
- AdobeClip
- 알고리즘
- 청년내일채움공제
- EH
- Vlogr
- InShot
- TKD
- android
- 동기메시징기술
- 쇼코의미소
- 수학 #다이나믹프로그래밍 #게임이론
- 레포지토리패턴
- 내채공만기
- 코딩테스트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함