无法解决android.view.ViewRoot $ CalledFromWrongThreadException。需要帮助?

时间:2011-10-28 07:03:47

标签: android exception

搜索后我发现有些手机(如Galaxy)受到一个奇怪的bug或任何导致此错误的因素的影响:

                            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中的应用程序中使用的Timer,我试图设置一个处理程序,但我在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时我找不到其他地方。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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