3

我正在 Android 系统上开发一个涉及解析一些 xml 内容的应用程序。问题是程序崩溃了,我还没有发现问题。我有一个 Activity,它有一个扩展 asynctask 的私有类。这是代码:

URL url = new URL(XML_INIT_ADRESS);
XmlPullParser xpp =  XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(url.openConnection().getInputStream(), null);

int pullParserState = xpp.getEventType();

while (pullParserState != XmlPullParser.END_DOCUMENT){
    if (xpp.getName().equals("signupclients") && pullParserState == XmlPullParser.START_TAG){ //Error!!!               
        while(!xpp.getName().equals("signupclients") && pullParserState != XmlPullParser.END_TAG){
            if (xpp.getName().equals("client")) {
                clientNames.add(xpp.getAttributeValue(0));
            }
            pullParserState = xpp.next();
        }
        pullParserState = xpp.next();
    }
}    

如您所见,我已对第一个 if 语句进行了评论,如果您将其注释掉,则包含该代码的代码将起作用。我可以补充说它在 Eclipse 中并且我捕获了所有错误。

String XML_INIT_ADRESS = "http://www.johanstenberg.se/tests/app/start.xml"

它应该是一个有效的 XML 文档。请帮忙,因为我一无所知,但我想看到它与 XMLPullParser 一起工作。我在 onCreate() 方法中调用 asynctask,如下所示:

new UpdateDatabase().execute();

其中 UpdateDatabase 是扩展 Asynctask 的私有类的名称。

提前致谢!

日志猫:

12-05 18:38:43.664: W/KeyCharacterMap(525): No keyboard for id 0
12-05 18:38:43.664: W/KeyCharacterMap(525): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-05 18:38:44.854: I/AndroidRuntime(525): AndroidRuntime onExit calling exit(0)
12-05 18:38:45.154: D/dalvikvm(535): GC freed 750 objects / 54888 bytes in 51ms
12-05 18:38:45.584: W/dalvikvm(535): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
12-05 18:38:45.594: E/AndroidRuntime(535): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
12-05 18:38:45.604: E/AndroidRuntime(535): java.lang.RuntimeException: An error occured while executing doInBackground()
12-05 18:38:45.604: E/AndroidRuntime(535):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
1    2-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.lang.Thread.run(Thread.java:1096)
12-05 18:38:45.604: E/AndroidRuntime(535): Caused by: java.lang.NullPointerException
12-05 18:38:45.604: E/AndroidRuntime(535):  at com.discountapp.johanaugust.UpdatingDB$UpdateDatabase.doInBackground(UpdatingDB.java:119)
12-05 18:38:45.604: E/AndroidRuntime(535):  at com.discountapp.johanaugust.UpdatingDB$UpdateDatabase.doInBackground(UpdatingDB.java:1)
12-05 18:38:45.604: E/AndroidRuntime(535):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-05 18:38:45.604: E/AndroidRuntime(535):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-05 18:38:45.604: E/AndroidRuntime(535):  ... 4 more
12-05 18:38:45.654: I/dalvikvm(535): threadid=7: reacting to signal 3
12-05 18:38:45.654: E/dalvikvm(535): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
4

1 回答 1

0

如果您从这一行得到异常:

if(xpp.getName().equals("signupclients") && pullParserState == XmlPullParser.START_TAG)

那么我最好的猜测是xpp.getName() 正在返回 null,而您正试图.equals()从该 null 引用中调用。

在尝试使用它之前,您应该从XmlPullParser检查eventType 。看看JavaDoc中的这个片段:

         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }

如果没有更多细节(如堆栈跟踪),很难给出更明确的答案。

于 2011-12-05T17:47:56.373 回答