我是Android的新手。我尝试从头部优先Android开发教程中的rss feed获取数据。但是,当我运行应用程序时出现错误。 这是我遇到的错误
05-12 09:48:15.833: E/AndroidRuntime(2135): FATAL EXCEPTION: main
05-12 09:48:15.833: E/AndroidRuntime(2135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nasadailyimage/com.example.nasadailyimage.NasaDailyImage}: java.lang.NullPointerException
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.os.Looper.loop(Looper.java:130)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-12 09:48:15.833: E/AndroidRuntime(2135): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 09:48:15.833: E/AndroidRuntime(2135): at java.lang.reflect.Method.invoke(Method.java:507)
05-12 09:48:15.833: E/AndroidRuntime(2135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-12 09:48:15.833: E/AndroidRuntime(2135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-12 09:48:15.833: E/AndroidRuntime(2135): at dalvik.system.NativeStart.main(Native Method)
05-12 09:48:15.833: E/AndroidRuntime(2135): Caused by: java.lang.NullPointerException
05-12 09:48:15.833: E/AndroidRuntime(2135): at com.example.nasadailyimage.NasaDailyImage.resetDisplay(NasaDailyImage.java:74)
05-12 09:48:15.833: E/AndroidRuntime(2135): at com.example.nasadailyimage.NasaDailyImage.onCreate(NasaDailyImage.java:32)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-12 09:48:15.833: E/AndroidRuntime(2135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-12 09:48:15.833: E/AndroidRuntime(2135): ... 11 more
这是我的IotdHandler.java
package com.example.nasadailyimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.jar.Attributes;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class IotdHandler extends DefaultHandler {
private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss";
//private String url = "http://www.nasa.gov/rss/image_of_the_day.rss";
//"http://www.nasa.gov/rss/dyn/image_of_the_day.rss"
private boolean inUrl = false;
private boolean inTitle = false;
private boolean inDescription = false;
private boolean inItem = false;
private boolean inDate = false;
private Bitmap image = null;
private String title = null;
private StringBuffer description = new StringBuffer();
private String date = null;
public void processFeed() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(url).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) {}
}
private Bitmap getBitmap(String url) {
try {
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
} catch (IOException ioe) { return null; }
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals("url")) { inUrl = true; }
else { inUrl = false; }
if (localName.startsWith("item")) { inItem = true; }
else
if (inItem) {
if (localName.equals("title")) { inTitle = true; }
else { inTitle = false; }
if (localName.equals("description")) { inDescription = true; }
else { inDescription = false; }
if (localName.equals("pubDate")) { inDate = true; }
else { inDate = false; }
}
}
public void characters(char ch[], int start, int length) {
String chars = new String(ch).substring(start, start + length);
if (inUrl && url == null) { image = getBitmap(chars); }
if (inTitle && title == null) { title = chars; }
if (inDescription) { description.append(chars); }
if (inDate && date == null) { date = chars; }
}
public Bitmap getImage() { return image; }
/*public String getImage() { return image.toString(); }*/
public String getTitle() { return title; }
public StringBuffer getDescription() { return description; }
public String getDate() { return date; }
}
我在Activity类
中添加了一些代码@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_daily_image);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
IotdHandler handler = new IotdHandler();
handler.processFeed();
resetDisplay(handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
}
private void resetDisplay(String title, String date, Bitmap imageUrl, StringBuffer description) {
TextView titleView = (TextView) findViewById(R.id.imageTitle);
titleView.setText(title);
TextView dateView = (TextView) findViewById(R.id.imageDate);
dateView.setText(date);
ImageView imageView = (ImageView) findViewById(R.id.imageDisplay);
imageView.setImageBitmap(imageUrl);
TextView descriptionView = (TextView) findViewById(R.id.imageDescription);
descriptionView.setText(description);
}
如何解决此错误?