1

我正在使用自定义基本适配器从 rss 提要获取标题、链接、描述、发布日期、guid。我首先使用简单的数组适配器直接实现了应用程序,它运行良好。但是在使用自定义基本适配器实现它时,我遇到了应用程序停止工作错误。

这是我的 bean 类RssItem.java

package com.maulik.rss;

public class RssItem {


String _title;
String _link;
String _description;
String _pubdate;
String _guid;

// constructor
public RssItem(){

}

// constructor with parameters
public RssItem(String title, String link, String description, String pubdate, String guid){
    this._title = title;
    this._link = link;
    this._description = description;
    this._pubdate = pubdate;
    this._guid = guid;
}


public void setTitle(String title){
    this._title = title;
}

public void setLink(String link){
    this._link = link;
}

public void setDescription(String description){
    this._description = description;
}

public void setPubdate(String pubDate){
    this._pubdate = pubDate;
}


public void setGuid(String guid){
    this._guid = guid;
}

/**
 * All GET methods
 * */
public String getTitle(){
    return this._title;
}

public String getLink(){
    return this._link;
}

public String getDescription(){
    return this._description;
}

public String getPubdate(){
    return this._pubdate;
}

public String getGuid(){
    return this._guid;
}
}

这是我的自定义基础适配器类RssCustomListRssItemsAdapter

public class RssCustomListRssItemsAdapter extends BaseAdapter{
Context context;
List<RssItem> rssItems;
public RssCustomListRssItemsAdapter(Context context,List<RssItem>rssItems){
    this.context=context;
    this.rssItems=rssItems;
}

private class ViewHolder{
    TextView url,title,date,description;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return rssItems.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return rssItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return rssItems.indexOf(rssItems.get(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder=null;
    if(convertView==null){
        convertView=inflater.inflate(R.layout.rss_item_list_row, null);

        holder.url=(TextView) convertView.findViewById(R.id.page_url);
        holder.title=(TextView) convertView.findViewById(R.id.title);
        holder.date=(TextView) convertView.findViewById(R.id.pub_date);
        holder.description=(TextView) convertView.findViewById(R.id.link);

        convertView.setTag(holder);
    }else{
        holder=(ViewHolder) convertView.getTag();
    }

    RssItem item=rssItems.get(position);

    holder.url.setText(item.getLink());
    holder.title.setText(item.getTitle());
    holder.date.setText(item.getPubdate());
    holder.description.setText(item.getDescription());

    return convertView;
}

}

这是称为RSS_ListRSSItemsActivity的活动

public class RSS_ListRSSItemsActivity extends ActionBarActivity implements
    OnItemClickListener {
ActionBar ab;

private ProgressDialog pDialog;
RssCustomListRssItemsAdapter adapter;
List<RssItem> rssItem = new ArrayList<RssItem>();
RssParser rssParser = new RssParser();
RssFeed rssFeed;
ListView lv;

String title, description, link, pubdate, guid;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss_item_list);

    ab = getSupportActionBar();

    Intent i = getIntent();
    Integer site_id = Integer.parseInt(i.getStringExtra("id"));
    String actionBar_title = i.getStringExtra(title);
    ab.setTitle(actionBar_title);

    // Getting Single website from SQLite
    RssDatabaseHandler rssDB = new RssDatabaseHandler(
            getApplicationContext());

    RssWebSite site = rssDB.getSite(site_id);
    String rss_link = site.getRSSLink();



    new loadRSSFeedItems().execute(rss_link);

    // selecting single ListView item
    lv = (ListView) findViewById(R.id.list);

    lv.setOnItemClickListener(this);

}

/**
 * Background Async Task to get RSS Feed Items data from URL
 * */
class loadRSSFeedItems extends AsyncTask<String, String, String> {

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

    /**
     * getting all recent articles and showing them in listview
     * */
    @Override
    protected String doInBackground(String... args) {
        // rss link url
        String rss_url = args[0];

        // list of rss items
        rssItem = rssParser.getRSSFeedItems(rss_url);

        // looping through each item
        for (RssItem item : rssItem) {

            item.getTitle();
            item.getLink();
            item.getDescription();
            item.getPubdate();
            item.getGuid();

            rssItem.add(item);


        }

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

                adapter = new RssCustomListRssItemsAdapter(RSS_ListRSSItemsActivity.this, rssItem);
                lv.setAdapter(adapter);


            }
        });
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String args) {

    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    Intent in = new Intent(getApplicationContext(),
            RSS_DetailedRSSActivity.class);

    // getting page url
    String page_url = ((TextView) view.findViewById(R.id.page_url))
            .getText().toString();
    String title = ((TextView) view.findViewById(R.id.title)).getText()
            .toString();
    String date = ((TextView) view.findViewById(R.id.pub_date)).getText()
            .toString();
    String description = ((TextView) view.findViewById(R.id.link))
            .getText().toString();
    in.putExtra("page_url", page_url);
    in.putExtra("title_url", title);
    in.putExtra("date_url", date);
    in.putExtra("description_url", description);
    startActivity(in);
}

}

这是我的日志猫

I/Choreographer(18848):跳过了 137 帧!应用程序可能在其主线程上做了太多工作。W / dalvikvm(18848):threadid = 12:线程退出未捕获的异常(组= 0x40b89378)致命异常:AsyncTask#2 java.lang.RuntimeException:在android.os.AsyncTask $ 3.done执行doInBackground()时发生错误(AsyncTask.java:299) 在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 在 java.util.concurrent.FutureTask.setException(FutureTask.java:124) 在 java.util.concurrent。 FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java。 util.concurrent.ThreadPoolExecutor。

第 88 行是for (RssItem item : rssItem) { 也在预执行中我使用了进度对话框,我得到了泄漏的窗口错误。我确实花了一整天的时间来弄清楚这一点并在网上使用各种答案。请帮忙

4

2 回答 2

1

这可能是问题的一部分。在doInBackground()

// updating UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {

AyncTask具有处理此类内容的方法,例如onProgressUpdate()可以从doInBackground()with调用publishProgress()。或者onPostExecute()也将是一个好地方。

另外,我不知道那些 RSS 方法是做什么的,但如果它是繁重的,那么它们应该被移动到你的AsyncTask或不同的Thread.

于 2013-09-10T17:47:41.733 回答
0

我解决了我的错误。我创建了类型的新实例

列出<RssItem> 项;

在 doInBackground 的 for 循环中,我将所有文本添加到这个新创建的 List 的对象“itemss”中。

然后就用了

rssItems.add(items);

这基本上解决了我在设置custombaseadapter时遇到的问题!

于 2013-09-15T19:50:41.680 回答