使用SAXParser获取字符串列表到arraylist的问题

时间:2010-06-21 20:00:18

标签: xml android arraylist saxparser

我正在尝试使用SAXParser解析Web服务响应并获取某些值,将它们存储到arraylist中,然后在列表视图中显示它们。

从Web服务返回的示例XML:

<ArrayOfStrings> <string>value</string> <string>value</string> <string>value</string> </ArrayOfStrings>

这是我的SAXHandler类:

public class SAXHandler extends DefaultHandler {

 private boolean outertag = false;
 private boolean mytag = false;
 ArrayList<String> alist;



 public ArrayList<String> getList(){
  return this.alist;
 }

 @Override
 public void startDocument() throws SAXException {
         }
 @Override
 public void endDocument() throws SAXException{

 }


 //@Override
      public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
 if (localName.equals("ArrayOfString")){
  this.outertag=true;
 }else if (localName.equals("string")){
  this.mytag=true;
 }

         }

 @Override 
    public void characters(char ch[], int start, int length) {
  if (this.mytag){
   String s = new String(ch, start, length);
   alist.add(s);
  }

    }


 @Override
      public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
  if (localName.equals("ArrayOfString")){
   this.outertag=false;
  }else if (localName.equals("string")){
   this.mytag=false;
  }

         }
}

这是我调用和恢复我的网络服务的方法:

try{
  URL url = new URL("xxxxxxx");

  SAXParserFactory spf = SAXParserFactory.newInstance();
     SAXParser sp = spf.newSAXParser();
     XMLReader xr = sp.getXMLReader();
     SAXHandler SAXHandler =  new SAXHandler();
     xr.setContentHandler(SAXHandler);
     xr.parse(new InputSource(url.openStream()));
     ArrayList<String> stateList = SAXHandler.getList();
     aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stateList);
     RESTlist.setAdapter(aa);

我一直有异常,我不知道为什么。任何人都可以帮我一把吗?

logcat的:

06-21 20:10:47.901: ERROR/AndroidRuntime(472): Uncaught handler: thread main exiting due to uncaught exception
06-21 20:10:47.950: ERROR/AndroidRuntime(472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pa.passammain/com.pa.passammain.RESTtest}: java.lang.NullPointerException: println needs a message
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.os.Looper.loop(Looper.java:123)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread.main(ActivityThread.java:4203)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at java.lang.reflect.Method.invoke(Method.java:521)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at dalvik.system.NativeStart.main(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): Caused by: java.lang.NullPointerException: println needs a message
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.util.Log.println(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.util.Log.e(Log.java:208)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at com.pa.passammain.RESTtest.onCreate(RESTtest.java:82)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-21 20:10:47.950: ERROR/AndroidRuntime(472):     ... 11 more

编辑我的错误在我的try / catch中显示的方式后,我不再获得异常,只是一个空白屏幕。这是我的logcat显示的内容:

06-21 20:22:00.330: WARN/System.err(542): java.lang.NullPointerException
06-21 20:22:00.352: WARN/System.err(542):     at com.pa.passammain.SAXHandler.characters(SAXHandler.java:45)
06-21 20:22:00.352: WARN/System.err(542):     at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:166)
06-21 20:22:00.362: WARN/System.err(542):     at org.apache.harmony.xml.ExpatParser.append(Native Method)
06-21 20:22:00.371: WARN/System.err(542):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
06-21 20:22:00.371: WARN/System.err(542):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
06-21 20:22:00.371: WARN/System.err(542):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
06-21 20:22:00.381: WARN/System.err(542):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
06-21 20:22:00.381: WARN/System.err(542):     at com.pa.passammain.RESTtest.onCreate(RESTtest.java:71)
06-21 20:22:00.391: WARN/System.err(542):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-21 20:22:00.391: WARN/System.err(542):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-21 20:22:00.401: WARN/System.err(542):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-21 20:22:00.401: WARN/System.err(542):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-21 20:22:00.410: WARN/System.err(542):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-21 20:22:00.410: WARN/System.err(542):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 20:22:00.420: WARN/System.err(542):     at android.os.Looper.loop(Looper.java:123)
06-21 20:22:00.420: WARN/System.err(542):     at android.app.ActivityThread.main(ActivityThread.java:4203)
06-21 20:22:00.420: WARN/System.err(542):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 20:22:00.430: WARN/System.err(542):     at java.lang.reflect.Method.invoke(Method.java:521)
06-21 20:22:00.430: WARN/System.err(542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-21 20:22:00.442: WARN/System.err(542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-21 20:22:00.442: WARN/System.err(542):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

看起来您没有分配alist的实例,如

alist = new ArrayList<String>();