如何将xml数据解析为listView

时间:2014-02-04 19:11:17

标签: android xml xml-parsing

我用xml文件和android愚弄了但是后来我发现了一些我不太了解的东西。我试图从xml文件中获取信息,并将其粘贴到我的Android应用程序中的listView中,但我得到一个runTime异常。我知道错误发生在我的XMLParser中,但我找不到我,我在这里搜索了它周围的主题。你能看看它并告诉我出了什么问题吗? (如果出现重大错误,请不要心脏病发作,这是我第一次使用xml解析器...)

package com.flamand.parksniffer.eventhandling;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.DropBoxManager.Entry;
import android.util.Xml;

public class XmlParser {
    private static XmlPullParser parser = Xml.newPullParser();

    private static InputStreamHandler inputStream;

    private String url = "http://193.190.76.149:81/ParkoParkings/states.php";

    private static String[] capaciteit;

    private XmlPullParserFactory factory() {
        XmlPullParserFactory fact = null;
        try {
            fact = XmlPullParserFactory.newInstance();
        } catch (XmlPullParserException e) {
            System.out.println("XML_PULL_PARSER_FACTORY_ERROR");
        }
        return fact;
    }

    public void parse() throws XmlPullParserException,
            IOException {
        parser.setInput(inputStream.XMLCode(url), null);
        int i = 0;
        while (parser.nextTag() != XmlPullParser.END_TAG) {
            if(parser.getName() == "parking"){
                capaciteit[i] = parser.getAttributeValue(null, "capaciteit");
                i++;
            }else{
                skip(parser);
            }
        }
    }

        private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                throw new IllegalStateException();
            }
            int depth = 1;
            while (depth != 0) {
                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
                }
            }
         }

        public static String[] getCapaciteit(){
            return capaciteit;
        }
}

class InputStreamHandler {

    public InputStream XMLCode(String url) throws IOException {
        InputStream in = null;
        XmlParser parser;

        in = downloadUrl(url);

        return in;
    }

    private InputStream downloadUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        return conn.getInputStream();
    }

}

这是使用这个类的片段(所以也许bug就在这里......

package com.flamand.parksniffer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

package com.flamand.parksniffer.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.flamand.parksniffer.R;
import com.flamand.parksniffer.eventhandling.XmlParser;

public class EventsEventFragment extends Fragment{

    View view;
    String capaciteit[];
    ListView list;
    Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view = inflater.inflate(R.layout.event_frag_layout, container, false);
        registerThings();
        loadPage();
        return view;
    }

    private void registerThings() {
        list = (ListView)view.findViewById(R.id.events_list);

    }

    private void loadPage(){
        capaciteit = XmlParser.getCapaciteit();
        for(int i = 0 ; i < capaciteit.length; i++){
            //button.setText(capaciteit[i]);
            //list.addHeaderView(button);
        }
    }



}

抱歉忘记了logcat(感谢您的评论)

02-04 20:14:41.411: E/AndroidRuntime(2216): FATAL EXCEPTION: main
02-04 20:14:41.411: E/AndroidRuntime(2216): Process: com.flamand.parksniffer, PID: 2216
02-04 20:14:41.411: E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flamand.parksniffer/com.flamand.parksniffer.Events}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.os.Looper.loop(Looper.java:136)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at java.lang.reflect.Method.invoke(Method.java:515)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at dalvik.system.NativeStart.main(Native Method)
02-04 20:14:41.411: E/AndroidRuntime(2216): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.Activity.setContentView(Activity.java:1929)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.flamand.parksniffer.Events.onCreate(Events.java:19)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.Activity.performCreate(Activity.java:5231)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-04 20:14:41.411: E/AndroidRuntime(2216):     ... 11 more
02-04 20:14:41.411: E/AndroidRuntime(2216): Caused by: java.lang.NullPointerException
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.flamand.parksniffer.fragments.EventsEventFragment.loadPage(EventsEventFragment.java:38)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at com.flamand.parksniffer.fragments.EventsEventFragment.onCreateView(EventsEventFragment.java:27)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-04 20:14:41.411: E/AndroidRuntime(2216):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-04 20:14:41.411: E/AndroidRuntime(2216):     ... 21 more
02-04 20:15:01.431: E/qcom_sensors_hal(751): hal_process_report_ind: Bad item quality: 11 

0 个答案:

没有答案