android.view.ViewRootImpl $CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图[重复]
Posted
技术标签:
【中文标题】android.view.ViewRootImpl $CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图[重复]【英文标题】:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views [duplicate] 【发布时间】:2013-09-21 04:08:19 【问题描述】:我遇到了一个导致我的应用程序强制关闭的问题,我不确定为什么会发生这种情况。我的 logcat 和源代码附在下面。如果需要任何其他信息,请告诉我。
LOGCAT:
09-16 17:28:22.881: I/dalvikvm(18145): Total arena pages for JIT: 11
09-16 17:28:22.991: I/dalvikvm(18145): Total arena pages for JIT: 12
09-16 17:28:23.461: I/dalvikvm(18145): Total arena pages for JIT: 13
09-16 17:28:23.461: I/dalvikvm(18145): Total arena pages for JIT: 14
09-16 17:28:23.521: I/System.out(18145): Arrest No.
09-16 17:28:23.521: W/dalvikvm(18145): threadid=11: thread exiting with uncaught exception (group=0x41785300)
09-16 17:28:23.531: E/androidRuntime(18145): FATAL EXCEPTION: AsyncTask #1
09-16 17:28:23.531: E/AndroidRuntime(18145): java.lang.RuntimeException: An error occured while executing doInBackground()
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.lang.Thread.run(Thread.java:856)
09-16 17:28:23.531: E/AndroidRuntime(18145): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.widget.TextView.checkForRelayout(TextView.java:6309)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.widget.TextView.setText(TextView.java:3547)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.widget.TextView.setText(TextView.java:3405)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.widget.TextView.setText(TextView.java:3380)
09-16 17:28:23.531: E/AndroidRuntime(18145): at com.example.test.MainActivity$MyTask.doInBackground(MainActivity.java:59)
09-16 17:28:23.531: E/AndroidRuntime(18145): at com.example.test.MainActivity$MyTask.doInBackground(MainActivity.java:1)
09-16 17:28:23.531: E/AndroidRuntime(18145): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-16 17:28:23.531: E/AndroidRuntime(18145): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-16 17:28:23.531: E/AndroidRuntime(18145): ... 5 more
来源:
public class MainActivity extends Activity
TextView tv;
final String URL="http://example.com";
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
private class MyTask extends AsyncTask<String, Void, String>
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute()
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
@Override
protected String doInBackground(String... params)
try
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for( Element element : tableHeader.children() )
// Here you can do something with each element
System.out.println(element.text());
tv.setText(element.text());
title = doc.title();
catch (IOException e)
e.printStackTrace();
return title;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
【问题讨论】:
【参考方案1】:你不能在 doInBackground 中使用tv.setText(element.text());
。
更一般地说,您不能在 doInBackground 方法中更新 UI 元素。
尝试探索名为onProgressUpdate
的方法。
我希望这会有所帮助。
【讨论】:
以上是关于android.view.ViewRootImpl $CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图[重复]的主要内容,如果未能解决你的问题,请参考以下文章
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@e48ad
Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for thi
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi