我不得不说,在我决定做一些修改之前,它的工作是完全正常的。然而,我所做的修改造成了许多错误,所以我试图逐一取消所有修改。我尽力将它恢复到前一个版本,但还是给我“NullPointerException”的“NullPointerExpendion”,无论是在读RSS () 或从AsyncTash () 调用到 showRSS () 中,这些都来自 AsyncTash () 。 之前的版本根本没有发生! 我调试了几个小时, 仍然无法找到线索。 任何帮助都会感激!
我的代码如下:
package com.android.rss;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class RSSProActivity extends ListActivity {
ProgressDialog progDialog;
String url ="";
ArrayList<RSSItem> feed;
ArrayList<String> links;
ArrayList<String> titles;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle bundle = this.getIntent().getExtras();
super.onCreate(savedInstanceState);
setContentView(R.layout.rss);
url = bundle.getString("add");
startLoadRSS(url);
}
private void startLoadRSS(String url){
new RSS_Load().execute(url);
}
//
private void preReadRSS()
{
// Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progDialog.setTitle("Loading RSS");
progDialog.setMessage("Please wait...");
progDialog.show();
}
private ArrayList<String> readRSS(String url)
{
String title ="";
String link ="";
String date ="";
titles = new ArrayList<String>();
titles.add("KNS");
// Initializing instance variables
//feed = new ArrayList<RSSItem>();
try{
URL linkUrl = new URL(url);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(linkUrl), "UTF_8");
/* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
* However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
* As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
* so we should skip the "<title>" tag which is a child of "<channel>" tag,
* and take in consideration only "<title>" tag which is a child of "<item>"
*
* In order to achieve this, we will make use of a boolean variable.
*/
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
titles.add(xpp.nextText()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return titles;
}
private InputStream getInputStream(URL url)
{
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
private void displayRSS()
{
progDialog.dismiss();
// String str [] = (String []) headlines.toArray (new String [headlines.size()]);
TextView textView = new TextView(this);
// int num = titles.size();
// String n = Integer.toString(num);
// textView.setText(n + "items found.");
// Data binding
try{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, titles);
setListAdapter(adapter);
}catch(NullPointerException e)
{
e.printStackTrace();
}
Toast.makeText(this, url, Toast.LENGTH_LONG).show();
}
/**
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(position)));
startActivity(browserIntent);
}
**/
//Asynchronous Task for downloading the RSS Feed.
public class RSS_Load extends AsyncTask<String, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
preReadRSS();
}
@Override
protected Void doInBackground(String... url) {
readRSS(url[0]);
return null;
}
@Override
protected void onProgressUpdate(Void... progress) {
// progDialog.setProgress();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
displayRSS();
}
//display progress bar here.
public void displayProgressBar(String status){
//todo
}
}
}
logCat 文件 :
05-25 06:07:13.406: D/dalvikvm(331): GC_EXTERNAL_ALLOC freed 49K, 53% free 2550K/5379K, external 1625K/2137K, paused 43ms
05-25 06:07:13.586: D/dalvikvm(331): GC_EXTERNAL_ALLOC freed 1K, 53% free 2549K/5379K, external 2563K/3200K, paused 40ms
05-25 06:07:29.416: W/dalvikvm(331): threadid=9: thread exiting with uncaught exception (group=0x40015560)
05-25 06:07:29.456: E/AndroidRuntime(331): FATAL EXCEPTION: AsyncTask #1
05-25 06:07:29.456: E/AndroidRuntime(331): java.lang.RuntimeException: An error occured while executing doInBackground()
05-25 06:07:29.456: E/AndroidRuntime(331): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.lang.Thread.run(Thread.java:1019)
05-25 06:07:29.456: E/AndroidRuntime(331): Caused by: java.lang.NullPointerException
05-25 06:07:29.456: E/AndroidRuntime(331): at com.android.rss.RSSProActivity.readRSS(RSSProActivity.java:88)
05-25 06:07:29.456: E/AndroidRuntime(331): at com.android.rss.RSSProActivity.access$1(RSSProActivity.java:54)
05-25 06:07:29.456: E/AndroidRuntime(331): at com.android.rss.RSSProActivity$RSS_Load.doInBackground(RSSProActivity.java:154)
05-25 06:07:29.456: E/AndroidRuntime(331): at com.android.rss.RSSProActivity$RSS_Load.doInBackground(RSSProActivity.java:1)
05-25 06:07:29.456: E/AndroidRuntime(331): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-25 06:07:29.456: E/AndroidRuntime(331): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-25 06:07:29.456: E/AndroidRuntime(331): ... 4 more
05-25 06:07:31.809: E/WindowManager(331): Activity com.android.rss.RSSProActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40526cd0 that was originally added here
05-25 06:07:31.809: E/WindowManager(331): android.view.WindowLeaked: Activity com.android.rss.RSSProActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40526cd0 that was originally added here
05-25 06:07:31.809: E/WindowManager(331): at android.view.ViewRoot.<init>(ViewRoot.java:258)
05-25 06:07:31.809: E/WindowManager(331): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
05-25 06:07:31.809: E/WindowManager(331): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-25 06:07:31.809: E/WindowManager(331): at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-25 06:07:31.809: E/WindowManager(331): at android.app.Dialog.show(Dialog.java:241)
05-25 06:07:31.809: E/WindowManager(331): at com.android.rss.RSSProActivity.preReadRSS(RSSProActivity.java:51)
05-25 06:07:31.809: E/WindowManager(331): at com.android.rss.RSSProActivity.access$0(RSSProActivity.java:44)
05-25 06:07:31.809: E/WindowManager(331): at com.android.rss.RSSProActivity$RSS_Load.onPreExecute(RSSProActivity.java:149)
05-25 06:07:31.809: E/WindowManager(331): at android.os.AsyncTask.execute(AsyncTask.java:391)
05-25 06:07:31.809: E/WindowManager(331): at com.android.rss.RSSProActivity.startLoadRSS(RSSProActivity.java:40)
05-25 06:07:31.809: E/WindowManager(331): at com.android.rss.RSSProActivity.onCreate(RSSProActivity.java:36)
05-25 06:07:31.809: E/WindowManager(331): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 06:07:31.809: E/WindowManager(331): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-25 06:07:31.809: E/WindowManager(331): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-25 06:07:31.809: E/WindowManager(331): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-25 06:07:31.809: E/WindowManager(331): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-25 06:07:31.809: E/WindowManager(331): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 06:07:31.809: E/WindowManager(331): at android.os.Looper.loop(Looper.java:123)
05-25 06:07:31.809: E/WindowManager(331): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-25 06:07:31.809: E/WindowManager(331): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 06:07:31.809: E/WindowManager(331): at java.lang.reflect.Method.invoke(Method.java:507)
05-25 06:07:31.809: E/WindowManager(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-25 06:07:31.809: E/WindowManager(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-25 06:07:31.809: E/WindowManager(331): at dalvik.system.NativeStart.main(Native Method)