我无法使用 Jsoup 获得 Google 主页的标题。我得到FATAL EXCEPTION: Thread
。
日志猫:
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;
}
}
新的错误日志:
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
我怎样才能解决这个问题?