gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:04:28 24 4
gpt4 key购买 nike

搜索后我发现某些手机(如 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 日志中看到了很多崩溃。

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

            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 时找不到其他地方。

最佳答案

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

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

关于android - 无法解决 android.view.ViewRoot$CalledFromWrongThreadException。需要帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com