Android - XML Parser App
I'm developing an android app which parses an XML file from the server using SAX and displays in the form of list. I'm getting null pointer exception. But when i excecuted the same code as java application removing all the android stuffs i'm able to print the list which wanted to display in the list. It's document of about 700 items. I couldn't understand much from the logs i got using Logcat. I'm new to android development. It would really helpful if you can help me with this.
06-08 13:27:04.032: ERROR/AndroidRuntime(403): FATAL EXCEPTION: main
06-08 13:27:04.032: ERROR/AndroidRuntime(403): java.lang.RuntimeException: Unable to 开发者_JAVA百科start activity ComponentInfo{rss.com/rss.com.RSSReader}: java.lang.NullPointerException
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.os.Looper.loop(Looper.java:123)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread.main(ActivityThread.java:3647)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at java.lang.reflect.Method.invoke(Method.java:507)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at dalvik.system.NativeStart.main(Native Method)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): Caused by: java.lang.NullPointerException
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at rss.com.RSSReader.onCreate(RSSReader.java:33)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
06-08 13:27:04.032: ERROR/AndroidRuntime(403): ... 11 more
My Manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rss.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".RSSReader"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowDescription" >
</activity>
</application>
</manifest>
Java code:
public class RSSReader extends Activity implements OnItemClickListener{
/** Called when the activity is first created. */
public RSSFeed feed=null;
public List<RSSItem> list = null;
public final static String RSSFEEDOFCHOICE = "http://people.rit.edu/~vxr9024/chemicalFeed.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
feed = getFeed(RSSFEEDOFCHOICE);
list = feed.getAllItems();
UpdateDisplay(list);
}
RSSFeed getFeed(String urlToRssFeed)
{
FeedParser parsedFeed = new FeedParser();
try {
URL url = new URL(urlToRssFeed);
XMLReader xread = XMLReaderFactory.createXMLReader();
xread.setContentHandler(parsedFeed);
InputSource is = new InputSource(url.openStream());
xread.parse(is);
return parsedFeed.getFeed();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}catch (Exception e){
System.out.println("Blahblah");
return null;
}
}
private void UpdateDisplay(List<RSSItem> list) {
// TODO Auto-generated method stub
RSSItem[] items = new RSSItem[list.size()];
list.toArray(items);
ListView itemlist = (ListView) findViewById(R.id.itemlist);
//RSSItem[] items = new RSSItem[()];
ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this, android.R.layout.simple_list_item_1,
items);
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
Intent intentItem = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("chemicalName",feed.getItem(position).getChemicalName());
b.putString("synonyms",feed.getItem(position).getSynonyms());
b.putString("formula",feed.getItem(position).getFormula());
intentItem.putExtra("android.intent.extra.INTENT", b);
startActivity(intentItem);
}
}
Maybe you need
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Otherwise the app can't use Internet to read you rss ?
精彩评论