1
我正在開發一個android應用程序,它使用SAX解析服務器中的XML文件並以列表的形式顯示。我收到空指針異常。但是,當我執行與java應用程序相同的代碼時,刪除所有android的東西,我可以打印列表中想要顯示的列表。這是大約700個項目的文件。 我無法從使用Logcat的日誌中瞭解太多。我是Android開發新手。如果你能幫助我,這真的很有幫助。Android - XML解析器應用程序
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 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
我的清單文件
<?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代碼:
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);
}
}
你有一個NullPointerException異常藏在那裏 13:27:04.032:ERROR/AndroidRuntime(403):06-08顯示java.lang.NullPointerException 13:27:04.032:由造成錯誤/ AndroidRuntime(403):在rss.com.RSSReader.onCreate(RSSReader.java:33) – 2011-06-08 13:43:09
feed = getFeed(RSSFEEDOFCHOICE); list = feed.getAllItems(); 從不同類中解析的項目列表。 – Vignesh 2011-06-08 13:47:53
似乎getFeed()返回null,你必須檢查爲什麼。 – Egor 2011-06-08 13:52:03