我无法使用Jsoup获得Google主页的标题。我得到了FATAL EXCEPTION: Thread
。
logcat的:
10-19 05:46:44.153: E/AndroidRuntime(597): FATAL EXCEPTION: Thread-75
10-19 05:46:44.153: E/AndroidRuntime(597): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:701)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.View.requestLayout(View.java:12555)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.View.requestLayout(View.java:12555)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.View.requestLayout(View.java:12555)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.View.requestLayout(View.java:12555)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.view.View.requestLayout(View.java:12555)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.widget.TextView.checkForRelayout(TextView.java:6716)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.widget.TextView.setText(TextView.java:3256)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.widget.TextView.setText(TextView.java:3110)
10-19 05:46:44.153: E/AndroidRuntime(597): at android.widget.TextView.setText(TextView.java:3085)
10-19 05:46:44.153: E/AndroidRuntime(597): at com.example.spravochnik05.MainActivity$1.run(MainActivity.java:34)
10-19 05:46:44.153: E/AndroidRuntime(597): at java.lang.Thread.run(Thread.java:856)
代码:
public class MainActivity extends Activity {
private TextView textView;
public Document doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
doc = Jsoup.connect("http://google.com").get();
textView.setText(doc.title());
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
新错误logcat:
10-19 06:08:28.302: E/AndroidRuntime(846): FATAL EXCEPTION: main
10-19 06:08:28.302: E/AndroidRuntime(846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spravochnik05/com.example.spravochnik05.MainActivity}: android.os.NetworkOnMainThreadException
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.os.Looper.loop(Looper.java:137)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-19 06:08:28.302: E/AndroidRuntime(846): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 06:08:28.302: E/AndroidRuntime(846): at java.lang.reflect.Method.invoke(Method.java:511)
10-19 06:08:28.302: E/AndroidRuntime(846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-19 06:08:28.302: E/AndroidRuntime(846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-19 06:08:28.302: E/AndroidRuntime(846): at dalvik.system.NativeStart.main(Native Method)
10-19 06:08:28.302: E/AndroidRuntime(846): Caused by: android.os.NetworkOnMainThreadException
10-19 06:08:28.302: E/AndroidRuntime(846): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
10-19 06:08:28.302: E/AndroidRuntime(846): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-19 06:08:28.302: E/AndroidRuntime(846): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-19 06:08:28.302: E/AndroidRuntime(846): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-19 06:08:28.302: E/AndroidRuntime(846): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-19 06:08:28.302: E/AndroidRuntime(846): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
10-19 06:08:28.302: E/AndroidRuntime(846): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
10-19 06:08:28.302: E/AndroidRuntime(846): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
10-19 06:08:28.302: E/AndroidRuntime(846): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
10-19 06:08:28.302: E/AndroidRuntime(846): at com.example.spravochnik05.MainActivity.onCreate(MainActivity.java:26)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.Activity.performCreate(Activity.java:4465)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-19 06:08:28.302: E/AndroidRuntime(846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-19 06:08:28.302: E/AndroidRuntime(846): ... 11 more
我该如何解决这个问题?
答案 0 :(得分:3)
textView.setText(doc.title());
删除此行。您无法从线程更新UI。而是使用runOnUIThread()
,或使用线程处理程序。
最佳选择是使用Android推荐的AsyncTask
。
以下是AsyncTask的使用情况,该{{3}}执行doInBackground() method
中的后台操作,并使用onPostExecute
方法更新用户界面。
以下是使用AsyncTask
:
在onCreate
方法中:
new DownloadFilesTask().execute();
在onCreate
结束后添加此类:
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
doc = Jsoup.connect("http://google.com").get();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (doc != null) {
textView.setText(doc.title());
}
else {
textView.setText("Doc not found");
}
}
}
答案 1 :(得分:2)
在日志中:
CalledFromWrongThreadException:仅创建的原始线程 视图层次结构可以触及其视图。
表示您正在尝试从非ui线程更新UI元素,因此请使用AsyncTask或runOnUiThread来获取非ui线程中的更新UI元素。
使用runOnUiThread
:
.....
try {
doc = Jsoup.connect("http://google.com").get();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(doc.title());
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...
答案 2 :(得分:0)
您无法在非UI线程中更改TextView
的文本。尝试在非UI线程中的runOnUIThread(Runnable)
中使用Activity
方法与UI线程进行通信
答案 3 :(得分:0)
答案 4 :(得分:0)
从你的线程中获取代码,并将其放在AsyncTask线程中:
class DownloadFile extends AsyncTask<Void, Void, Void> {
@Override
protected String doInBackground(String... arg0) {
your code
}
}
某些操作(例如发出HTTP请求)不允许在主UI线程上运行。
执行:
new DownloadFile().execute()