我已经从Google Alerts Source制作了一个 Android RSS Feed 应用程序。在我编译并运行后只显示空白屏幕但不崩溃。您可以从我的脚本中检查出什么问题,如下所示:
来自 ListRSSItemsActivity.java:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class ListRSSItemsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Array list for list view
ArrayList<HashMap<String, String>> rssItemList = new ArrayList<HashMap<String,String>>();
RSSParser rssParser = new RSSParser();
// button add new website
ImageButton btnAddSite;
List<RSSItem> rssItems = new ArrayList<RSSItem>();
RSSFeed rssFeed;
private static String TAG_TITLE = "title";
private static String TAG_LINK = "link";
private static String TAG_DESRIPTION = "description";
private static String TAG_PUB_DATE = "pubDate";
private static String TAG_GUID = "guid"; // not used
private String urlString = "http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178";
@Override
public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.rss_item_list);
String str = urlString;
new loadRSSFeed().execute(str);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
{
Intent localIntent = new Intent(ListRSSItemsActivity.this.getApplicationContext(), DisPlayWebPageActivity.class);
localIntent.putExtra("page_url", ((TextView)paramView.findViewById(R.id.page_url)).getText().toString());
ListRSSItemsActivity.this.startActivity(localIntent);
}
});
}
class loadRSSFeed extends AsyncTask<String, String, String>
{
loadRSSFeed()
{
}
protected String doInBackground(String[] paramArrayOfString)
{
String str1 = paramArrayOfString[0];
ListRSSItemsActivity.this.rssFeed = ListRSSItemsActivity.this.rssParser.getRSSFeed(str1);
Log.d("rssFeed", "rssFeed: " + ListRSSItemsActivity.this.rssFeed);
Log.d("rssFeed", "url: " + str1);
if (ListRSSItemsActivity.this.rssFeed != null)
{
Log.e("RSS URL", ListRSSItemsActivity.this.rssFeed.getTitle() + ListRSSItemsActivity.this.rssFeed.getLink() + ListRSSItemsActivity.this.rssFeed.getDescription() + ListRSSItemsActivity.this.rssFeed.getLanguage());
WebSite localWebSite = new WebSite(ListRSSItemsActivity.this.rssFeed.getTitle(), ListRSSItemsActivity.this.rssFeed.getLink(), ListRSSItemsActivity.this.rssFeed.getRSSLink(), ListRSSItemsActivity.this.rssFeed.getDescription());
HashMap localHashMap = new HashMap();
localHashMap.put(ListRSSItemsActivity.TAG_TITLE, localWebSite.getTitle());
localHashMap.put(ListRSSItemsActivity.TAG_LINK, localWebSite.getLink());
String str2 = localWebSite.getDescription();
if (str2.length() > 100)
str2 = str2.substring(0, 97) + "..";
localHashMap.put(ListRSSItemsActivity.TAG_DESRIPTION, str2);
ListRSSItemsActivity.this.rssItemList.add(localHashMap);
}
ListRSSItemsActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
ListRSSItemsActivity localListRSSItemsActivity = ListRSSItemsActivity.this;
ArrayList localArrayList = ListRSSItemsActivity.this.rssItemList;
String[] arrayOfString = new String[4];
arrayOfString[0] = ListRSSItemsActivity.TAG_LINK;
arrayOfString[1] = ListRSSItemsActivity.TAG_TITLE;
arrayOfString[2] = ListRSSItemsActivity.TAG_PUB_DATE;
arrayOfString[3] = ListRSSItemsActivity. TAG_DESRIPTION;
SimpleAdapter localSimpleAdapter = new SimpleAdapter(localListRSSItemsActivity, localArrayList, R.layout.rss_item_list_row, arrayOfString, new int[] { R.id.page_url, R.id.title , R.id.pub_date, R.id.link });
ListRSSItemsActivity.this.setListAdapter(localSimpleAdapter);
}
});
return null;
}
protected void onPostExecute(String paramString)
{
ListRSSItemsActivity.this.pDialog.dismiss();
}
protected void onPreExecute()
{
super.onPreExecute();
ListRSSItemsActivity.this.pDialog = new ProgressDialog(ListRSSItemsActivity.this);
ListRSSItemsActivity.this.pDialog.setMessage("Fetching RSS Information ...");
ListRSSItemsActivity.this.pDialog.setIndeterminate(false);
ListRSSItemsActivity.this.pDialog.setCancelable(false);
ListRSSItemsActivity.this.pDialog.show();
}
}
}
来自 RSSParser.java
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class RSSParser
{
private static String TAG_CHANNEL = "channel";
private static String TAG_DESRIPTION;
private static String TAG_GUID;
private static String TAG_ITEM;
private static String TAG_LANGUAGE;
private static String TAG_LINK;
private static String TAG_PUB_DATE;
private static String TAG_TITLE = "title";
static
{
TAG_LINK = "link";
TAG_DESRIPTION = "content";
TAG_LANGUAGE = "language";
TAG_ITEM = "entry";
TAG_PUB_DATE = "published";
TAG_GUID = "guid";
}
private String setTime(String paramString)
{
String str1 = paramString.substring(0, 10) + " " + paramString.substring(11, 19);
Log.d("date", str1);
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
String str2 = localSimpleDateFormat.format(new Date(25200000L + localSimpleDateFormat.parse(str1).getTime()));
return str2;
}
catch (ParseException localParseException)
{
localParseException.printStackTrace();
}
return str1;
}
public Document getDomElement(String paramString)
{
DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
InputSource localInputSource = new InputSource();
localInputSource.setCharacterStream(new StringReader(paramString));
Document localDocument = localDocumentBuilder.parse(localInputSource);
return localDocument;
}
catch (ParserConfigurationException localParserConfigurationException)
{
Log.e("Error: ", localParserConfigurationException.getMessage());
return null;
}
catch (SAXException localSAXException)
{
Log.e("Error: ", localSAXException.getMessage());
return null;
}
catch (IOException localIOException)
{
Log.e("Error: ", localIOException.getMessage());
}
return null;
}
public final String getElementValue(Node paramNode)
{
if ((paramNode != null) && (paramNode.hasChildNodes()));
for (Node localNode = paramNode.getFirstChild(); ; localNode = localNode.getNextSibling())
{
if (localNode == null)
return "";
if ((localNode.getNodeType() == 3) || (localNode.getNodeType() == 4))
return localNode.getNodeValue();
}
}
public RSSFeed getRSSFeed(String paramString)
{
if (paramString != null)
{
String str1 = getXmlFromUrl(paramString);
if (str1 != null)
try
{
Element localElement = (Element)getDomElement(str1).getElementsByTagName(TAG_CHANNEL).item(0);
String str2 = getValue(localElement, TAG_TITLE);
String str3 = getValue(localElement, TAG_LINK);
RSSFeed localRSSFeed = new RSSFeed(str2, getValue(localElement, TAG_DESRIPTION), str3, paramString, getValue(localElement, TAG_LANGUAGE));
return localRSSFeed;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
return null;
}
public List<RSSItem> getRSSFeedItems(String paramString)
{
ArrayList localArrayList = new ArrayList();
String str1 = getXmlFromUrl(paramString);
if (str1 != null)
try
{
NodeList localNodeList1 = getDomElement(str1).getElementsByTagName(TAG_ITEM);
for (int i = 0; ; i++)
{
if (i >= localNodeList1.getLength())
return localArrayList;
Element localElement = (Element)localNodeList1.item(i);
String str2 = Html.fromHtml(getValue(localElement, TAG_TITLE)).toString();
NodeList localNodeList2 = localElement.getElementsByTagName(TAG_LINK);
localElement.getAttribute("href");
localArrayList.add(new RSSItem(str2, ((Element)localNodeList2.item(0)).getAttribute("href").split("&")[0].replace("https://www.google.com/url?q=", ""), Html.fromHtml(getValue(localElement, TAG_DESRIPTION)).toString(), setTime(getValue(localElement, TAG_PUB_DATE)), getValue(localElement, TAG_GUID)));
}
}
catch (Exception localException)
{
localException.printStackTrace();
}
return localArrayList;
}
public String getValue(Element paramElement, String paramString)
{
return getElementValue(paramElement.getElementsByTagName(paramString).item(0));
}
public String getXmlFromUrl(String paramString)
{
try
{
String str = EntityUtils.toString(new DefaultHttpClient().execute(new HttpGet(paramString)).getEntity());
return str;
}
catch (UnsupportedEncodingException localUnsupportedEncodingException)
{
localUnsupportedEncodingException.printStackTrace();
return null;
}
catch (ClientProtocolException localClientProtocolException)
{
localClientProtocolException.printStackTrace();
return null;
}
catch (IOException localIOException)
{
localIOException.printStackTrace();
}
return null;
}
}
来自 rss_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<!-- Header -->
<include layout="@layout/header"/>
<ListView
android:id="@android:id/list"
android:layout_below="@id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#bababa"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="#ffffff"
android:cacheColorHint="#00000000"/>
</RelativeLayout>
来自 rss_item_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dip" >
<TextView android:id="@+id/page_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<!-- Article title -->
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="1dip"
android:textSize="18dip"
android:textStyle="bold"
android:textColor="#303030" />
<!-- published date -->
<TextView
android:id="@+id/pub_date"
android:layout_below="@id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="3dip"
android:textSize="14dip"
android:textColor="#b70400"/>
<!-- article description -->
<TextView android:id="@+id/link"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingBottom="6dip"
android:textSize="15dip"
android:textColor="#8d8d8d"
android:layout_below="@id/pub_date"/>
</RelativeLayout>
我的错误日志:
10-12 21:29:26.353: W/System.err(1697): java.lang.NullPointerException
10-12 21:29:26.353: W/System.err(1697): at com.a.b.RSSParser.getValue(RSSParser.java:157)
10-12 21:29:26.353: W/System.err(1697): at com.a.b.RSSParser.getRSSFeed(RSSParser.java:116)
10-12 21:29:26.353: W/System.err(1697): at com.a.b.ListRSSItemsActivity$loadRSSFeed.doInBackground(ListRSSItemsActivity.java:75)
10-12 21:29:26.363: W/System.err(1697): at com.a.b.ListRSSItemsActivity$loadRSSFeed.doInBackground(ListRSSItemsActivity.java:1)
10-12 21:29:26.363: W/System.err(1697): at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-12 21:29:26.363: W/System.err(1697): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-12 21:29:26.363: W/System.err(1697): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-12 21:29:26.363: W/System.err(1697): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-12 21:29:26.363: W/System.err(1697): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-12 21:29:26.363: W/System.err(1697): at java.lang.Thread.run(Thread.java:841)
10-13 09:00:01.940: D/rssFeed(1816): rssFeed: null
10-13 09:00:01.940: D/rssFeed(1816): url: http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178
我该如何解决?