SpeechRecognizer should be used only from the application's main thread
Android/공개글 2014. 12. 21. 18:23SpeechRecognizer 객체의 startListening 메서드를 사용하려는 도중 다음과 같은 에러가 발생하였다.
SpeechRecognizer는 오직 애플리케이션의 메인 스레드로부터만 사용되어야 한다 라는 것인데..
startListening 메서드를 호출하는 부분을 아래처럼 바꾸어주었더니 이유는 잘 모르겠지만 일단 문제가 해결되었다.
----- 코드 선언부분 -----
Handler handler;
Runnable runn;
----- 초기화 부분 -----
handler = new Handler();
runn = new Runnable() {
@Override
public void run() {
speechRecognizer.startListening(intent);
}
};
----- startListening 메서드를 호출했던 원래 코드 부분 -----
//speechRecognizer.startListening(intent);
handler.post(runn);
-----
안드로이드 API 문서에서 핸들러에 해당하는 문서를 읽어보면,
Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it
handler 객체는 자신을 생성하는 쓰레드에 바인드되는 그런 객체라고 한다. 그래서 startListening 메서드를 직접
실행하는 대신 handler 객체에게 실행해달라고 요청하는 식으로 코드를 변경했더니 나의 경우 문제가 해결되었다.
근데 한 가지 궁금한건.. SpeechRecognizer를 애플리케이션의 메인 스레드로부터 사용하라고 하는데 이 때의 메인
스레드가 정확히 무엇을 의미하는지? 잘 모르겠다.
'Android > 공개글' 카테고리의 다른 글
구글 플레이 스토어에 앱 등록 전 점검 사항 (1) | 2015.01.11 |
---|---|
failed to rename directory unable to resolve target (0) | 2014.12.27 |
android error error retrieving parent for item no resource found that matches the given name (0) | 2014.12.02 |
java.net.ConnectException: failed to connect to: connect failed: EHOSTUNREACH (No route to host) (1) | 2014.11.29 |
android sqlite no such table (3) | 2014.11.17 |