Sax字符串输入的解析器

时间:2013-09-05 13:51:49

标签: android string parsing sax

我正在尝试使用SaxParser解析xml文件。我用url和xml文件完成了这个。但现在我想从字符串中解析它。

这是字符串:

String xml_string = "<?xml version="1.0" standalone="yes"?> <NewDataSet> <Table> <expnr>672 </expnr> <artnr>ML-824          </artnr> </Table> <Table> <expnr>672 </expnr> <artnr>ML-824          </artnr> </Table> </NewDataSet> "

现在我需要解析那个字符串,我试过这个:

InputStream stream = new ByteArrayInputStream(xml_string.getBytes());
                    items = SAXXMLParser.parse(stream);

但它不起作用(在arrayadapter中出错)

这是错误:

3654-3654/E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
    at android.widget.ListView.onMeasure(ListView.java:1158)
    at android.view.View.measure(View.java:15518)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15518)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
    ... 36 more

解析此问题的正确方法是什么?

编辑:我发现了我做错了什么

我正在使用此代码:

InputStream stream = new ByteArrayInputStream(resultFull.getBytes());
                items = SAXXMLParser.parse(stream);
                ArrayAdapter adapter= new ArrayAdapter(activty, R.layout.list, items);

                ListView lv = (ListView) findViewById(R.id.listView);
                lv.setAdapter(adapter);

但我没有像我这样使用我的CustomListViewAdapter:

InputStream stream = new ByteArrayInputStream(resultFull.getBytes());
                items = SAXXMLParser.parse(stream);
                CustomListViewAdapter listViewAdapter = new CustomListViewAdapter(activty, items);

                ListView lv = (ListView) findViewById(R.id.listView);
                lv.setAdapter(listViewAdapter);

使用最后一个代码,它正在运行。这是非常愚蠢但我找到了解决方案!谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

错误表示您正在尝试将realativelayout转换为textview某处。检查你的onCreate并查看你在那里声明的所有relativelayout和textview。