2013-04-27 41 views
0

我看jsoup庫和我它是如何工作的寫這個簡單的代碼看:Jsoup和線程錯誤

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    downloadThread.start(); 

} 

Thread downloadThread = new Thread() { 
    @Override 
    public void run() { 
     TextView tw = (TextView) findViewById(R.text_view.tw1); 
     Document doc; 
     try { 
      doc = Jsoup.connect("http://www.google.it").get(); 
      tw.setText(doc.title()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}; 

而且我這個元素添加到AndroidManifest.xml中

<uses-permission 
    android:name="android.permission.INTERNET"/> 

但是當我啓動軟件,我已經相繼式的錯誤:

04-27 17:25:49.801: E/AndroidRuntime(26035): FATAL EXCEPTION: Thread-2004 
04-27 17:25:49.801: E/AndroidRuntime(26035): 
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that 
created a view hierarchy can touch its views. 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:854) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.view.ViewGroup.invalidateChild(ViewGroup.java:4075) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.view.View.invalidate(View.java:10386) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.view.View.invalidate(View.java:10341) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.widget.TextView.checkForRelayout(TextView.java:6439) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at  
android.widget.TextView.setText(TextView.java:3696) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.widget.TextView.setText(TextView.java:3554) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
android.widget.TextView.setText(TextView.java:3529) 
04-27 17:25:49.801: E/AndroidRuntime(26035): at 
com.example.webparsingtest.MainActivity$1.run(MainActivity.java:30) 

回答

2

從線程diff的,你不能訪問到UI組件不在主線程

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new ExampleAsyncTask().execute(); 
    } 

    static class ExampleAsyncTask extends AsyncTask<Void, Void, String> { 

     @Override 
     protected String doInBackground(Void... unused) { 
      Document doc; 
      try { 
       doc = Jsoup.connect("http://www.google.it").get(); 
       return doc.title(); 
      } catch (IOException e) { 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(String title) { 

      if(title == null){ 
       Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      TextView tw = (TextView) findViewById(R.text_view.tw1); 
      tw.setText(title); 
     } 
    } 

} 
+0

謝謝,它的工作原理。 – giozh 2013-04-27 16:22:12