Reputation: 1919
this code should parse the xml and show it ! but it has exeption :
02-02 12:36:49.508: I/Choreographer(1184): Skipped 32 frames! The application may be doing too much work on its main thread. 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): at 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): at 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): at android.os.Looper.loop(Looper.java:137) 02-02 12:36:51.006: W/System.err(1184): at 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): Skipped 39 frames! The application may be doing too much work on its main thread. 02-02 12:36:53.596: D/dalvikvm(1184): GC_CONCURRENT freed 180K, 4% free 8234K/8519K, paused 96ms+114ms, total 356ms
I cant find the reason!!!!
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) {
}
}
}
Upvotes: 0
Views: 711
Reputation: 12919
LongOperation lOp = new LongOperation(this); lOp.execute("");
From this line, it seems you are filling nodeList
in an AsyncTask
, but instead of waiting for it to be filled, you start parsing it immediately. Make sure to start parsing from onPostExecuted
in your LongOperation
.
BTW, it would not be a bad idea to also do the parsing in the background.
Upvotes: 2