Hi I m writing an android application. I found a code snippet for xml parsing. I tested it on another project and everything worked fine. So I thought lets test it on my main project but then it gives me a nullpointer :S.
这是我的ParseXMLDemo阶级:
public class ParseXMLDemo extends ListActivity {
@SuppressWarnings("rawtypes")
private ArrayList vraag;
@SuppressWarnings("rawtypes")
private ArrayList a1;
private ArrayList p1;
private ArrayList a2;
private ArrayList p2;
private ArrayList a3;
private ArrayList p3;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = ParseXMLMethods.getXML();
Document doc = ParseXMLMethods.XMLfromString(xml);
int numResults = ParseXMLMethods.numResults(doc);
if((numResults <= 0)){
Toast.makeText(ParseXMLDemo.this, "Er is een fout opgetreden bij het updaten van de vragen. Probeert u het later opnieuw.", Toast.LENGTH_LONG).show();
finish();
iii
NodeList children = doc.getElementsByTagName("os");
for (int i = 0; i < children.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)children.item(i);
vraag.add(ParseXMLMethods.getValue(e, "vraag"));
a1.add(ParseXMLMethods.getValue(e, "a1"));
p1.add(ParseXMLMethods.getValue(e, "p1"));
a2.add(ParseXMLMethods.getValue(e, "a2"));
p2.add(ParseXMLMethods.getValue(e, "p2"));
a3.add(ParseXMLMethods.getValue(e, "a3"));
p3.add(ParseXMLMethods.getValue(e, "p3"));
Toast.makeText(ParseXMLDemo.this, (CharSequence) vraag.get(i)+" "+a1.get(i)+" "+p1.get(i)+" "+a2.get(i)+" "+p2.get(i)+" "+a3.get(i)+" "+p3.get(i), Toast.LENGTH_LONG).show();
map.put("id", ParseXMLMethods.getValue(e, "id"));
map.put("vraag", ParseXMLMethods.getValue(e, "vraag"));
map.put("a1", ParseXMLMethods.getValue(e, "a1"));
map.put("p1", ParseXMLMethods.getValue(e, "p2"));
map.put("a2", ParseXMLMethods.getValue(e, "a2"));
map.put("p2", ParseXMLMethods.getValue(e, "p2"));
map.put("a3", ParseXMLMethods.getValue(e, "a3"));
map.put("p3", ParseXMLMethods.getValue(e, "p3"));
mylist.add(map);
iii
Toast.makeText(ParseXMLDemo.this, "testje"+mylist.get(1), Toast.LENGTH_LONG).show();
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_layout,
new String[] { "vraag", "a1", "p1", "a2", "p2", "a3", "p3" iii,
new int[] { R.id.title, R.id.subtitle, R.id.sub1, R.id.sub11, R.id.sub2, R.id.sub22, R.id.sub3iii);
setListAdapter(adapter);
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(ParseXMLDemo.this,o.get("vraag"), Toast.LENGTH_LONG).show();
iii
iii);
iiiiii
这是我的ParseXMLMethods类:
public class ParseXMLMethods {
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
iii catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
iii catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
iii catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
iii
return doc;
iii
public final static String getElementValue( Node elem ) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
iii
iii
iii
iii
return "";
iii
public static String getXML(){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.oudersvragen.nl/test.xml");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
iii catch (Exception e) {
line = "Internet Connection Error >> " + e.getMessage();
iii
return line;
iii
public static int numResults(Document doc){
Node results = doc.getDocumentElement();
int res = -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
iiicatch(Exception e ){
res = -1;
iii
return res;
iii
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return ParseXMLMethods.getElementValue(n.item(0));
iii
iii
Can Somebody tell me why it gives me a nullpointer exception in an android 2.3.3 project and why everything works the way its supposed on an android 1.6 project? (FYI. I m running the project on the same android 4.0 vm)
物流输出:
05-05 20:50:33.561: E/AndroidRuntime(3705): FATAL EXCEPTION: main
05-05 20:50:33.561: E/AndroidRuntime(3705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hgs.database/com.hgs.database.ParseXMLDemoiii: java.lang.NullPointerException
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.os.Looper.loop(Looper.java:137)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 20:50:33.561: E/AndroidRuntime(3705): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:50:33.561: E/AndroidRuntime(3705): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 20:50:33.561: E/AndroidRuntime(3705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 20:50:33.561: E/AndroidRuntime(3705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 20:50:33.561: E/AndroidRuntime(3705): at dalvik.system.NativeStart.main(Native Method)
05-05 20:50:33.561: E/AndroidRuntime(3705): Caused by: java.lang.NullPointerException
05-05 20:50:33.561: E/AndroidRuntime(3705): at com.hgs.database.ParseXMLDemo.onCreate(ParseXMLDemo.java:57)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.Activity.performCreate(Activity.java:4465)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 20:50:33.561: E/AndroidRuntime(3705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)