0

我是安卓新手。我创建了一个用于 xml 解析的应用程序。但是出现了一些错误。它意外停止。我尽力了。但我无法解决问题。请帮助我。

package example.xmlparsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.sax.Element;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {


    static final String URL = "http://api.androidhive.info/pizza/?format=xml";
    static final String KEY_ITEM = "item"; 
    static final String KEY_ID = "id";
    static final String KEY_NAME = "name";
    static final String KEY_COST = "cost";
    static final String KEY_DESC = "description";


    ArrayList<HashMap<String,String> > menu=new ArrayList<HashMap<String,String>>();
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        XMLParser parser=new XMLParser();
        String xml=parser.getXmlFromUrl(URL);
        Document doc=parser.getDomelement(xml);

        NodeList n1=doc.getElementsByTagName(KEY_ITEM);

        for(int i=0;i<n1.getLength();i++){

              HashMap<String, String> mp=new HashMap<String, String>();

              Element e= (Element) n1.item(i);
              mp.put(KEY_ID,parser.getValue(e, KEY_ID));
              mp.put(KEY_NAME, parser.getValue(e, KEY_NAME));
              mp.put(KEY_COST,parser.getValue(e, KEY_COST));
              mp.put(KEY_DESC, parser.getValue(e, KEY_DESC));

              menu.add(mp);

        }

        ListAdapter adapter=new SimpleAdapter(this,menu,
                R.layout.list_item,
                new String[]{KEY_NAME,KEY_COST,KEY_DESC},new int[]{
                R.id.name, R.id.desciption, R.id.cost });

            setListAdapter(adapter);
              lv = getListView();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}



*********Android Manifest***********

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.xmlparsing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="example.xmlparsing.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

LOGCAT

07-17 12:21:20.770: D/AndroidRuntime(352): Shutting down VM
07-17 12:21:20.770: W/dalvikvm(352): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-17 12:21:20.820: E/AndroidRuntime(352): FATAL EXCEPTION: main
07-17 12:21:20.820: E/AndroidRuntime(352): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.xmlparsing/example.xmlparsing.MainActivity}: java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.os.Looper.loop(Looper.java:123)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 12:21:20.820: E/AndroidRuntime(352):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 12:21:20.820: E/AndroidRuntime(352):  at java.lang.reflect.Method.invoke(Method.java:521)
07-17 12:21:20.820: E/AndroidRuntime(352):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 12:21:20.820: E/AndroidRuntime(352):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 12:21:20.820: E/AndroidRuntime(352):  at dalvik.system.NativeStart.main(Native Method)
07-17 12:21:20.820: E/AndroidRuntime(352): Caused by: java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
07-17 12:21:20.820: E/AndroidRuntime(352):  at example.xmlparsing.MainActivity.onCreate(MainActivity.java:48)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-17 12:21:20.820: E/AndroidRuntime(352):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-17 12:21:20.820: E/AndroidRuntime(352):  ... 11 more
07-17 12:21:24.549: I/Process(352): Sending signal. PID: 352 SIG: 9
4

1 回答 1

0
import android.sax.Element;

...

Element e= (Element) n1.item(i);

org.w3c.dom.Element与 DOM 解析而不是 SAX 元素一起使用。

于 2013-10-23T10:38:46.143 回答