このURLのSAXを使用してxml解析を作成したい
http://news.google.com/?output=rss
しかし、エラーが発生しました。どこで間違いを犯したか、または問題の別の解決策があるかどうかを確認して知らせてください。
この行でGoogleNewsSaxActivity.classにエラーが発生しました
for (int i = 0; i < baseParserData.getTitle().size(); i++) {
私のコードは以下の通りです。
GoogleNewsSaxActivity.class
public class GoogleNewsSaxActivity extends Activity {
BaseFeedParser baseParserData;
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
private myAdapter rssadaptor = null;
ListView lview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("http://news.google.com/?output=rss");
RssHandler myXMLHandler = new RssHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
}
baseParserData = RssHandler.baseParserData;
/**
* Run a for loop to set All the TextViews with text until
* the size of the array is reached.
*
**/
for (int i = 0; i < baseParserData.getTitle().size(); i++) {
System.out.println("value of i==============>" + i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", baseParserData.getTitle().get(i));
//map.put("Artist", baseParserData.getArtist().get(i));
//map.put("Contry", baseParserData.getCountry().get(i));
items.add(map);
rssadaptor = new myAdapter(GoogleNewsSaxActivity.this, items);
}
lview = (ListView) findViewById(R.id.listView);
// Getting adapter by passing xml data ArrayList
rssadaptor = new myAdapter(this, items);
lview.setAdapter(rssadaptor);
}
}
RssHandler.class
public class RssHandler extends DefaultHandler{
/*private List<BaseFeedParser> messages;
//private Message currentMessage;
private StringBuilder builder;*/
String elementValue = null;
Boolean elementOn = false;
public static BaseFeedParser baseParserData = null;
public static BaseFeedParser getBaseParserData() {
return baseParserData;
}
public static void setBaseParserData(BaseFeedParser baseParserData) {
RssHandler.baseParserData = baseParserData;
}
/*public List<BaseFeedParser> getMessages(){
return this.messages;
}*/
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
elementOn = true;
System.out.println("Hello this is startElement : =========>");
if (localName.equals(BaseFeedParser.CHANNEL))
{
baseParserData = new BaseFeedParser();
} else if (localName.equals(BaseFeedParser.ITEM)) {
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
elementOn = false;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase(BaseFeedParser.TITLE))
baseParserData.setTitle(elementValue);
else if (localName.equalsIgnoreCase(BaseFeedParser.LINK))
baseParserData.setLink(elementValue);
else if (localName.equalsIgnoreCase(BaseFeedParser.PUB_DATE))
baseParserData.setPubDate(elementValue);
else if (localName.equalsIgnoreCase(BaseFeedParser.DESCRIPTION))
baseParserData.setDescription(elementValue);
}
}
また、別の2つのクラスBaseFeedParser.classとmyAdapter.classがあります。BaseFeedParserクラスにはGetters / Settersメソッドが含まれ、myadapterはカスタムリストビューのアダプタークラスです。
ありがとう。
LOGCATにエラーを表示する
05-31 09:42:25.693: I/System.out(279): java.net.SocketException: Permission denied
05-31 09:42:25.713: D/AndroidRuntime(279): Shutting down VM
05-31 09:42:25.713: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-31 09:42:25.752: E/AndroidRuntime(279): FATAL EXCEPTION: main
05-31 09:42:25.752: E/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rahul.android.sax/com.rahul.android.sax.GoogleNewsSaxActivity}: java.lang.NullPointerException
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 09:42:25.752: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 09:42:25.752: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
05-31 09:42:25.752: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 09:42:25.752: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 09:42:25.752: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
05-31 09:42:25.752: E/AndroidRuntime(279): Caused by: java.lang.NullPointerException
05-31 09:42:25.752: E/AndroidRuntime(279): at com.rahul.android.sax.GoogleNewsSaxActivity.onCreate(GoogleNewsSaxActivity.java:61)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-31 09:42:25.752: E/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-31 09:42:25.752: E/AndroidRuntime(279): ... 11 more