簡體   English   中英

java.lang.NullPointerException xml解析

[英]java.lang.NullPointerException xml parsing

這段代碼應該解析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):at 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):at android .app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)02-02 12:36:50.966:W / System.err(1184):at 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):at android .app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)02-02 12:36:50.986:W / System.err(1184):at 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) {
            }
        }

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

從這一行開始,您似乎在AsyncTask中填充nodeList ,但不是等待它被填充,而是立即開始解析它。 確保從onPostExecuted中的LongOperation開始解析。

順便說一句,在后台進行解析也不是一個壞主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM