我正在使用自定义基本适配器从 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) { 也在预执行中我使用了进度对话框,我得到了泄漏的窗口错误。我确实花了一整天的时间来弄清楚这一点并在网上使用各种答案。请帮忙