2014-02-21 88 views
0

我正在連接到一個網站,我從哪裏獲取新聞並將它們放入列表中。當我點擊新聞(標題)時,我想轉到另一個頁面,在那裏我可以看到新聞的內容。我正在使用jsoup來做這件事。 Logcat不會給我任何錯誤,但當我點擊標題時,應用程序會崩潰。使用jsoup訪問新聞的內容

這裏是我的活動:

公共類TargetActivity1延伸活動{ 的ListView LISTA;

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.web); 

    Parse parsing = new Parse(); 
    parsing.execute(); 

} 

private class Parse extends AsyncTask<Void, Void, Void> { 

    String desc; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      // Connect to the web site 
      Document document = Jsoup.connect(
        "http://www.polimi.it/en/news/").get(); 
      // Using Elements to get the Meta data 
      Elements description = document.select("div .news-list-item a"); 
      // Locate the content attribute 
      desc = description.attr("href"); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Set description into TextView 
     WebView txtdesc = (WebView) findViewById(R.id.webView2); 

     txtdesc.addView(txtdesc); 

     Intent i = new Intent(Intent.ACTION_VIEW); 
     i.setData(Uri.parse(desc)); 
     startActivity(i); 
     txtdesc.removeAllViews(); 

    } 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 

     Intent myIntent = new Intent(getApplicationContext(), 
       MainActivity.class); 
     startActivityForResult(myIntent, 0); 

     return true; 

    } 
    return super.onOptionsItemSelected(item); 
} 

}

+0

如果你的應用程序崩潰了,你應該在logcat中有一些東西 – nikis

+0

是的。它說:找不到處理意圖的活動{act = android.intent.action.VIEW dat =(我從中解析的網站)}。所以問題與意圖,但我不明白爲什麼。 – user2928230

回答

0

Parse類是錯誤的,它應該是這樣的:

private class Parse extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 
     String desc = ""; 
     try { 
      // Connect to the web site 
      Document document = Jsoup.connect(
        "http://www.polimi.it/en/news/").get(); 
      // Using Elements to get the Meta data 
      Elements description = document.select("div .news-list-item a"); 
      // Locate the content attribute 
      desc = description.attr("href"); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return desc; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // Set description into TextView 
     WebView txtdesc = (WebView) findViewById(R.id.webView2); 

     txtdesc.addView(txtdesc); // adding txtdesc to txtdesc? it's wrong, bug 

     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(result)); 
     startActivity(i); 

     txtdesc.removeAllViews(); 

    } 
} 

請閱讀有關的AsyncTask這裏http://developer.android.com/reference/android/os/AsyncTask.html。如上所述,doInBackground的結果可以傳遞給onPostExecute。另外請注意,你正在試圖增加一個視圖,我已經評論它。