java.lang.NullPointerException xml解析

时间:2014-02-02 08:55:14

标签: java android xml parsing nullpointerexception

此代码应该解析xml并显示它!但它有例外:

  

02-02 12:36:49.508:I / Choreographer(1184):跳过32帧!应用程序可能在其主线程上做了太多工作。   02-02 12:36:50.926:W / System.err(1184):java.lang.NullPointerException   02-02 12:36:50.926:W / System.err(1184):at com.example.sample.itcuties.android.reader.ITCutiesReaderAppActivity.onCreate(ITCutiesReaderAppActivity.java:52)   02-02 12:36:50.946:W / System.err(1184):在android.app.Activity.performCreate(Activity.java:5008)   02-02 12:36:50.946:W / System.err(1184):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)   02-02 12:36:50.946:W / System.err(1184):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)   02-02 12:36:50.966:W / System.err(1184):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)   02-02 12:36:50.966:W / System.err(1184):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)   02-02 12:36:50.986:W / System.err(1184):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)   02-02 12:36:50.986:W / System.err(1184):在android.os.Handler.dispatchMessage(Handler.java:99)   02-02 12:36:50.998:W / System.err(1184):在android.os.Looper.loop(Looper.java:137)   02-02 12:36:51.006:W / System.err(1184):在android.app.ActivityThread.main(ActivityThread.java:4745)   02-02 12:36:51.018:W / System.err(1184):at java.lang.reflect.Method.invokeNative(Native Method)   02-02 12:36:51.026:W / System.err(1184):at java.lang.reflect.Method.invoke(Method.java:511)   02-02 12:36:51.026:W / System.err(1184):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)   02-02 12:36:51.036:W / System.err(1184):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)   02-02 12:36:51.036:W / System.err(1184):at dalvik.system.NativeStart.main(Native Method)   02-02 12:36:51.046:I / System.out(1184):xml activity Excpetion = java.lang.NullPointerException   02-02 12:36:51.736:I / Choreographer(1184):跳过39帧!应用程序可能在其主线程上做了太多工作。   02-02 12:36:53.596:D / dalvikvm(1184):GC_CONCURRENT释放180K,4%自由8234K / 8519K,暂停96ms + 114ms,总计356ms

我找不到原因!!!!

package com.example.sample.itcuties.android.reader;


import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Main application activity.
 * 
 * @author ITCuties
 *
 */
public class ITCutiesReaderAppActivity extends Activity {
    NodeList nodeList;
    /** 
     * This method creates main application view
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set view

        try{


        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
         /** Create a new textview array to display the results */
        TextView name[];
        TextView website[];
        TextView category[];

        LongOperation lOp = new LongOperation(this);
        lOp.execute("");

         if(nodeList.getLength() > 0)
         {
             name = new TextView[nodeList.getLength()];
            website = new TextView[nodeList.getLength()];
            category = new TextView[nodeList.getLength()];

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

                Node node = nodeList.item(i);

                name[i] = new TextView(this);
                website[i] = new TextView(this);
                category[i] = new TextView(this);

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("name");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = fstElmnt.getElementsByTagName("website");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();
                website[i].setText("Website = " + ((Node) websiteList.item(0)).getNodeValue());

                category[i].setText("Website Category = " + websiteElement.getAttribute("category"));

                layout.addView(name[i]);
                layout.addView(website[i]);
                layout.addView(category[i]);
            }


         }

    /** Set the layout view to display */
            setContentView(layout);

         } catch (Exception e) {
             System.out.println("xml activity Excpetion = " + e);
         }
    }
     private class LongOperation extends AsyncTask<String, Void, String> {

         Context LongOperation;

            public LongOperation(Context context){
                   this.LongOperation = context;
                 }
            @Override
            protected String doInBackground(String... params) {


                try {

                    URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(new InputSource(url.openStream()));
                    doc.getDocumentElement().normalize();

                    nodeList = doc.getElementsByTagName("item");

                } catch (Exception e) {
                    System.out.println("XML Pasing Excpetion = " + e);
                }


                return null;
            }        

            @Override
            protected void onPostExecute(String result) {             
            }

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected void onProgressUpdate(Void... values) {
            }
        }

}

1 个答案:

答案 0 :(得分:2)

LongOperation lOp = new LongOperation(this); lOp.execute(""); 

从这一行开始,您似乎在nodeList填充AsyncTask,但不是等待它被填充,而是立即开始解析它。请务必从onPostExecuted中的LongOperation开始解析。

顺便说一下,在后台进行解析也不是一个坏主意。