问题描述
我有这导致我的应用程序强制关闭的问题,我不知道为什么这可能发生。我logcat的和源附后。请让我知道是否需要任何附加信息。
logcat的:
17 09-16:28:22.881:I / dalvikvm(18145):总的舞台页JIT:11
09-16 17:28:22.991:I / dalvikvm(18145):总的舞台页JIT:12
09-16 17:28:23.461:I / dalvikvm(18145):总的舞台页JIT:13
09-16 17:28:23.461:I / dalvikvm(18145):总的舞台页JIT:14
09-16 17:28:23.521:我/的System.out(18145):第逮捕
09-16 17:28:23.521:W / dalvikvm(18145):主题ID = 11:螺纹未捕获的异常(组= 0x41785300)退出
09-16 17:28:23.531:E / AndroidRuntime(18145):致命异常:AsyncTask的#1
09-16 17:28:23.531:E / AndroidRuntime(18145):了java.lang.RuntimeException:执行doInBackground发生错误()
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.os.AsyncTask $ 3.done(AsyncTask.java:299)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.lang.Thread.run(Thread.java:856)
09-16 17:28:23.531:E / AndroidRuntime(18145):$ android.view.ViewRootImpl CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程所致。
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.view.View.requestLayout(View.java:15129)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.widget.TextView.checkForRelayout(TextView.java:6309)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.widget.TextView.setText(TextView.java:3547)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.widget.TextView.setText(TextView.java:3405)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.widget.TextView.setText(TextView.java:3380)
09-16 17:28:23.531:E / AndroidRuntime(18145):在com.example.test.MainActivity $ MyTask.doInBackground(MainActivity.java:59)
09-16 17:28:23.531:E / AndroidRuntime(18145):在com.example.test.MainActivity $ MyTask.doInBackground(MainActivity.java:1)
09-16 17:28:23.531:E / AndroidRuntime(18145):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
09-16 17:28:23.531:E / AndroidRuntime(18145):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
09-16 17:28:23.531:E / AndroidRuntime(18145):... 5个
来源:
公共类MainActivity延伸活动{TextView的电视;
最终字符串URL =http://example.com;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);电视=(的TextView)findViewById(R.id.TextView01);
新MyTask()执行(URL);
}私有类MyTask扩展的AsyncTask<弦乐,太虚,字符串> {
ProgressDialog PROG;
字符串title =;
@覆盖
在preExecute保护无效(){
PROG =新ProgressDialog(MainActivity.this);
prog.setMessage(加载......);
prog.show();
}
@覆盖
保护字符串doInBackground(字符串... PARAMS){
尝试{
。文档的DOC = Jsoup.connect(PARAMS [0])获得();
元件的tableHeader = doc.select(TR)第一();
对于(element元素:tableHeader.children())
{
//在这里你可以做一些与每个元素
的System.out.println(element.text());
tv.setText(element.text());
} 标题= doc.title();
}赶上(IOException异常五){
e.printStackTrace();
}
返回称号;
}
@覆盖
保护无效onPostExecute(字符串结果){
super.onPostExecute(结果);
prog.dismiss();
tv.setText(结果);
}
}
}
您不能使用 tv.setText(element.text());
里面doInBackground。
在更一般的话,您将无法更新doInBackground方法内部的UI元素。
尝试探索调用的方法 onProgressUpdate
。
我希望这有助于。
I'm having an issue which causes my application to force close and I'm not sure why this might be happening. My logcat and source are attached below. Please let me know if any additional information is required.
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
SOURCE:
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);
}
}
}
You can't use tv.setText(element.text());
inside doInBackground.In a more general words, you can't update UI elements inside doInBackground method.Try exploring the method called onProgressUpdate
.
I hope this helps.
这篇关于android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!