【问题标题】:Cannot solve android.view.ViewRoot$CalledFromWrongThreadException. Help needed?无法解决 android.view.ViewRoot$CalledFromWrongThreadException。需要帮助?
【发布时间】:2011-10-28 07:03:47
【问题描述】:

经过搜索,我发现某些手机(如 Galaxy s)受到了一个奇怪的错误或任何导致此错误的影响:

                            java.lang.RuntimeException: An error occured while executing doInBackground()
                at android.os.AsyncTask$3.done(AsyncTask.java:200)
                at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
                at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
                at java.lang.Thread.run(Thread.java:1019)
            Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                at android.view.ViewRoot.checkThread(ViewRoot.java:2934)
                at android.view.ViewRoot.invalidateChild(ViewRoot.java:643)
                at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:669)
                at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
                at android.view.View.invalidate(View.java:5279)
                at android.webkit.WebView.selectionDone(WebView.java:4470)
                at android.webkit.WebView.clearHelpers(WebView.java:1295)
                at android.webkit.WebView.loadDataWithBaseURL(WebView.java:1714)
                at c.a(Unknown Source)
                at c.doInBackground(Unknown Source)
                at android.os.AsyncTask$2.call(AsyncTask.java:185)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
                ... 4 more
            android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                at android.view.ViewRoot.checkThread(ViewRoot.java:2934)
                at android.view.ViewRoot.invalidateChild(ViewRoot.java:643)
                at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:669)
                at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
                at android.view.View.invalidate(View.java:5279)
                at android.webkit.WebView.selectionDone(WebView.java:4470)
                at android.webkit.WebView.clearHelpers(WebView.java:1295)
                at android.webkit.WebView.loadDataWithBaseURL(WebView.java:1714)
                at c.a(Unknown Source)
                at c.doInBackground(Unknown Source)
                at android.os.AsyncTask$2.call(AsyncTask.java:185)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
                at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
                at java.lang.Thread.run(Thread.java:1019)

如果需要,这就是 logcat,从这里看来,这是滑动功能的问题:

            10-27 19:19:20.329 I/InputReader(  166): dispatchTouch::touch event's action is 1
            10-27 19:19:20.329 I/InputDispatcher(  166): Delivering touch to current input target: action: 1, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:20.749 I/InputReader(  166): dispatchTouch::touch event's action is 0
            10-27 19:19:20.749 I/InputDispatcher(  166): Delivering touch to current input target: action: 0, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:21.689 I/InputReader(  166): dispatchTouch::touch event's action is 1
            10-27 19:19:21.689 I/InputDispatcher(  166): Delivering touch to current input target: action: 1, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:21.689 W/webview ( 1125): Miss a drag as we are waiting for WebCore's response for touch down.
            10-27 19:19:21.989 I/InputReader(  166): dispatchTouch::touch event's action is 0
            10-27 19:19:21.989 I/InputDispatcher(  166): Delivering touch to current input target: action: 0, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:22.159 I/Ads     ( 1125): Refreshing ad.
            10-27 19:19:22.169 I/webclipboard( 1125): clipservice: android.sec.clipboard.ClipboardExManager@4051cb08
            10-27 19:19:22.259 I/Ads     ( 1125): adRequestUrlHtml: <html><head><script src="http://www.gstatic.com/afma/sdk-core-v40.js"></script><script>AFMA_buildAdURL({"preqs":15,"u_sd":1.0,"u_w":320,"slotname":"a14e098781b871d","msid":"***PACKEGE_NAME***","js":"afma-sdk-a-v4.0.4","isu":"F1B8EEEA64C5716BA996D22689C17557","format":"320x50_mb","net":"wi","app_name":"4.0.4.android.***PACKEGE_NAME***","hl":"pt","u_h":480,"u_so":"p","u_audio":1,"prl":792});</script></head><body></body></html>
            10-27 19:19:22.449 I/InputReader(  166): dispatchTouch::touch event's action is 1
            10-27 19:19:22.449 I/InputDispatcher(  166): Delivering touch to current input target: action: 1, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:22.449 W/webview ( 1125): Miss a drag as we are waiting for WebCore's response for touch down.
            10-27 19:19:22.599 I/Ads     ( 1125): Received ad url: <"url": "http://googleads.g.doubleclick.net:80/mads/gma?preqs=15&u_sd=1&u_w=320&slotname=a14e098781b871d&msid=***PACKEGE_NAME***&js=afma-sdk-a-v4.0.4&isu=F1B8EEEA64C5716BA996D22689C17557&format=320x50_mb&net=wi&app_name=4.0.4.android.***PACKEGE_NAME***&hl=pt&u_h=480&u_so=p&u_audio=1&prl=792&output=html&region=mobile_app&u_tz=240&ex=1&client_sdk=1&askip=0", "afmaNotifyDt": "null">
            10-27 19:19:22.609 I/InputReader(  166): dispatchTouch::touch event's action is 0
            10-27 19:19:22.609 I/InputDispatcher(  166): Delivering touch to current input target: action: 0, channel '4096f5c8 ***PACKEGE_NAME***/***PACKEGE_NAME***.Game (server)'
            10-27 19:19:23.339 W/dalvikvm( 1125): threadid=11: thread exiting with uncaught exception (group=0x40018578)
            10-27 19:19:23.339 E/ACRA    ( 1125): ACRA caught a RuntimeException exception for ***PACKEGE_NAME***. Building report.
            10-27 19:19:23.339 D/ACRA    ( 1125): Retrieve application default SharedPreferences.
            10-27 19:19:23.349 V/webview ( 1125): ZoomScale 3 mPreserveZoom: false
            10-27 19:19:23.489 I/ACRA    ( 1125): READ_LOGS granted! ACRA can include LogCat and DropBox data.
            10-27 19:19:23.519 D/ACRA    ( 1125): Retrieving logcat output...

唯一可能导致此错误的是我在游戏 Intent 的应用程序中使用的计时器,我尝试设置处理程序,但在我的 ACRA 日志中看到很多崩溃。

这是我的意图内容,希望有人能帮助我:)

            startB.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view) {

                            startB.setEnabled(false);
                            partiColTempo(tempox);
                        }
                    });

                        }


                 private void displayText(final String text){
                        this.runOnUiThread(new Runnable(){
                            //@Override
                            public void run() {
                                tempo.setText(text);

                                if (text.equals("Finished")){
                                    eseguiSuono();                      
                                    assegnaPunti();     
                                    startB.setEnabled(true);
                                    //elapsed=0;
                                    tempo.setText("Waiting");
                                }
                            }});
                    }

                private void partiColTempo(int tempox) {
                    TIMEOUT=tempox*1000;
                    Log.w("tempo timer", ""+tempox);
                    elapsed=INTERVAL;
                    TimerTask task=new TimerTask(){
                        @Override
                        public void run() {
                            elapsed+=INTERVAL;
                            if(elapsed>TIMEOUT){
                                this.cancel();
                                displayText("Finished");

                                return;
                            }
                            //if(some other conditions)
                            //   this.cancel();
                            displayText(getString(R.string.tempo_rim)+" " + (TIMEOUT-elapsed) / 1000);
                        }
                    };
                    Timer timer = new Timer();
                    timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);

有问题吗?因为我在调用 asynctask 时找不到其他地方。

【问题讨论】:

  • 从您的 logcat 帖子中,问题似乎出在 AsyncTask 中的某个地方 - 您是否正在使用这样的任务,如果是,您能否也发布代码?
  • 从 logcat 看来,您似乎使主线程外的某些视图无效,而这在 Android 中是不允许的。
  • 所以不是定时器的问题?我必须搜索 AsyncTask?我的应用程序中使用的唯一“任务”是那个计时器。
  • 看来您已经使用 displayText() 中的 runOnUIThread() - 这是正确的。请发布更多您的堆栈跟踪

标签: android exception


【解决方案1】:

您显然正在使用来自AsyncTask.doInBackground() 的 UI(即视图派生对象)做一些事情。这是不允许的,只有 UI 线程可以接触 UI 对象。

如果您需要在后台/AsyncTask 运行时更新 UI,您必须调用 publishProgress(),这将导致 onProgressUpdate 在 UI 线程上运行。您还可以实现onPostExecute(),一旦后台线程完成,它也会在 UI 线程上运行。

【讨论】:

  • 所以它不是计时器(具有相对速度的TextView)?我找不到它在哪里,有什么方法可以找到它吗?
  • 根据您的 logcat,错误位于混淆组件中,即它显示“在 c.a(未知来源),在 c.doInBackground(未知来源)”。您还在使用哪些其他库?
  • 也许我找到了解决方案,我有一个旧的 admob 库,我正在尝试一个新的。
【解决方案2】:

这种情况一般发生在AsyncTask线程在 背景,但与此同时,UI 活动类被破坏。所以 在完成AsyncTask 之后,当我们尝试执行一些 UI 任务时, 会抛出上述异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多