0

我尝试解析 aOnline XML File并将项目(子项目)写入 a Listview。但我得到一个java.lang.NullPointerException. 我知道错误消息并没有真正的帮助,但我现在已经确定了两个小时了这个问题......

我的代码:

        try {

        URL url;
        url = new URL("http://domain.tld/file.xml");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("item");

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("subitemname");
            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();
            valueList.add(nameList.item(0).getNodeValue());
            NodeList websiteList = fstElmnt.getElementsByTagName("subitem2");
            Element websiteElement = (Element) websiteList.item(0);
            websiteList = websiteElement.getChildNodes();
        }
        ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, valueList);
        final ListView lv = (ListView)findViewById(R.id.listview);
        lv.setAdapter(adapter);
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

堆栈跟踪:

10-07 23:44:00.460  13715-13715/com.app.name W/System.err﹕ java.lang.NullPointerException
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at com.app.name.add_series.onCreate(add_series.java:82)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5133)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.ActivityThread.access$600(ActivityThread.java:153)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5289)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-07 23:44:00.470  13715-13715/com.app.name W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
10-07 23:44:00.550  13715-13715/com.app.name W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection
10-07 23:44:00.550  13715-13715/com.app.name W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection

错误:I/System.out﹕ XML Pasing Excpetion = java.lang.NullPointerException

4

2 回答 2

1

试试这个..

    ListView list;

    ArrayList<String> title;


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);

        list = (ListView) findViewById(R.id.list);
        title = new ArrayList<String>();

         if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
        URL url;
        try {

            url = new URL(urls);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));

            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("Episode");

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

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("EpisodeName");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();

                title.add(""+ ((Node) nameList.item(0)).getNodeValue());

                System.out.println("title : "+((Node) nameList.item(0)).getNodeValue());

            }

            list.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,title));

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
于 2013-10-08T05:08:58.793 回答
0

valueList.add(nameList.item(0).getNodeValue());

您在哪里初始化了 valueList 这就是它抛出空指针异常的原因。

于 2013-10-08T05:35:45.423 回答