2014-12-29 116 views
0

我想建立一個RSS閱讀器,並把RSS提要獲取作爲ansyctask, ,返回列表視圖中的飼料,或返回一個文本視圖,說「沒有互聯網連接」 但應用程序仍然崩潰,我不知道有什麼問題,請你幫忙。Android AsyncTask活動崩潰RSS閱讀器

這裏是代碼:

package rss; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.enporan.polytechoran.R; 

public class RSSActivity extends ActionBarActivity { 
    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_news); 


      rssfeedget alpha = new rssfeedget(); 
      alpha.execute(); 



    } 

    private class rssfeedget extends AsyncTask<String, Void, FeedSource> { 


     protected void onPreExecute() { 

     } 

     @Override 
     protected FeedSource doInBackground(String... params) { 
      FeedSource f = new HttpFeedSource(); 
      if(f!=null) 
       return f; 
      else { 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(FeedSource result){ 
      ListView rssItemList = (ListView) findViewById(R.id.rssListview); 
      rssItemList.setVerticalFadingEdgeEnabled(true); 
      if(doInBackground()==null){ 
       TextView tv= (TextView) findViewById(R.id.textView2); 
       tv.setText("No internet Connection..."); 

      } 
      else{ 

       RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, doInBackground().getFeed()); 
       rssItemList.setAdapter(adapter); 
      } 

     } 
    } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_news, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 
+0

你會得到什麼樣的例外? (我猜這是一個networkonmainthreadexception) – viplezer

+0

@coelho:由於網絡代碼是在AsyncTask中執行的,因此不應該追加NetworkOnMainThreadException。 Larbi:你可以在你的文章中添加堆棧跟蹤,這樣我們可以幫助你嗎? – chteuchteu

+0

@chteuchteu這裏是錯誤: –

回答

0

下面是代碼:

private class rssfeedget extends AsyncTask<String, Void, List<RSSItem>> { 
    private List<RSSItem> result; 

    protected void onPreExecute() { 

    } 

    @Override 
    protected List<RSSItem> doInBackground(String... params) { 
     FeedSource f = new HttpFeedSource(); 
     if(f.getFeed()==null) 
      return null; 
     else { 
      this.result = f.getFeed(); // Execute getFeed in doInBackground 
      return result; 
     } 
    } 

    @Override 
    protected void onPostExecute(List<RSSItem> result){ 


     if(doInBackground()==null){ 
      TextView tv= (TextView) findViewById(R.id.textView2); 
      tv.setText("No internet Connection..."); 

     } 
     else{ 
      ListView rssItemList = (ListView) findViewById(R.id.rssListview); 
      rssItemList.setVerticalFadingEdgeEnabled(true); 
      RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, this.result); 
      rssItemList.setAdapter(adapter); 
     } 

    } 
} 
+0

@chteuchteu這裏是編輯的代碼,但給出了致命的錯誤 –

1

由於@coelho指出的那樣,FeedSource.getFeed()不應該在UI線程中執行。您現在必須在UI線程內執行onPreExecuteonPostExecute方法,而doInBackground方法不是。

以下是你可以做的事情:在你的AsyncTask類中,添加一個私有成員:private List<RSSItem> result;(在這裏用getFeed返回的集合類型替換RSSItem)。

然後,更新doInBackground

FeedSource f = new HttpFeedSource(); 
if (f != null) 
    return f; 
else { 
    this.result = f.getFeed(); // Execute getFeed in doInBackground 
    return null; 
} 

然後,在onPostExecute方法,你就可以使用這個私有成員,因爲這:

RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, this.result); 
+0

我申請,但它仍然崩潰時,沒有連接,,, 12-29 21:49:18.1-32316/com.enporan.polytechoran E/AndroidRuntime:致命異常:AsyncTask#1 過程:com.enporan.polytechoran ,PID:32101 –

+0

當手機沒有連接時,它是否會崩潰?或者是拋出NetworkOnMainThreadException異常? – chteuchteu