[ERROR/EXCEPTION] WebView CrossThreadException

2021. 6. 17. 10:28Android/개발 CASE

UI 변경 코드(반드시 UI thread만 변경가능)가 자바스크립트 인터페이스를 통한 실행에 포함되어 있어 크로스 스레드 오류가 난다. 예외 처리는 아래와 같이 해결하면 된다.

https://aorica.tistory.com/106

 

[ Android ] a webview method was called on thread 'javabridge'

안드로이드에서 자바스크립트를 호출할 때, A WebView method was called on thread 'javabridge'. All WebView methods must be called on the same thread.라는 메시지가 뜰 때 있습니다. 안드로이드에서 UI 스..

aorica.tistory.com

자바스크립트 인터페이스 함수 -> JavaBridge라는 thread(비UI thread)에서 수행

 

 public static void webBack(final WebView webview, final JSONObject args, final String callback) throws JSONException {
  webview.post(()->{
    HoneMobile.get().window().onBackPressed();

    resultCallBack(webview, callback, null, ResultCode.SUCCESS);
  });
}

 

여기서 post는 webview가 붙어있는 정보(mAttachedInfo)의 핸들러(mHandler)를 이용해 연결된 MessageQueue에 넣는 작업을 한다. (참고로 여기서의 mAttachedInfo.mHandler는 ZygoteInit에서 실행되는 ActivityThread에서 초기화되는 내용)

 /**
     * <p>Causes the Runnable to be added to the message queue.
     * The runnable will be run on the user interface thread.</p>
     *
     * @param action The Runnable that will be executed.
     *
     * @return Returns true if the Runnable was successfully placed in to the
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     *
     * @see #postDelayed
     * @see #removeCallbacks
     */
    public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().post(action);
        return true;
    }

https://itmining.tistory.com/5

 

[안드로이드] Thread, Handler, Looper를 통한 백그라운드 처리

이 글은 PC 버전 TISTORY에 최적화 되어있습니다. 서론  안드로이드의 UI는 기본적으로 메인스레드를 주축으로하는 싱글 스레드 모델로 동작하므로, 메인 스레드에서는 긴 작업을 피해야 합니다.

itmining.tistory.com

 

https://codetravel.tistory.com/16

 

View.post() 사용하기

지난 "runOnUiThread(Runnable) 사용하기" 포스팅을 통하여 UI 도구 키트를 UI thread에서 동작시킬 수 있는 방법을 알아 보았습니다. 이번에는 유사한 방법인 View.post(Runnable) API를 사용하여 동일한 결과를

codetravel.tistory.com