2013-03-18 44 views
1

我寫在我使用XMLParser的解析的DOM元素數據的程序,但每當我運行我的程序得到:不幸的是應用程序已經停止XML解析器DOM產生的原因:顯示java.lang.NullPointerException

logcat的說:

03-18 10:41:11.266: I/System.out(843): Getting DOM Element(s):- null 
03-18 10:41:11.266: D/AndroidRuntime(843): Shutting down VM 
03-18 10:41:11.266: W/dalvikvm(843): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 
03-18 10:41:11.287: E/AndroidRuntime(843): FATAL EXCEPTION: main 
03-18 10:41:11.287: E/AndroidRuntime(843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListViewPackage}: java.lang.NullPointerException 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.os.Looper.loop(Looper.java:137) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.main(ActivityThread.java:5039) 
03-18 10:41:11.287: E/AndroidRuntime(843): at java.lang.reflect.Method.invokeNative(Native Method) 
03-18 10:41:11.287: E/AndroidRuntime(843): at java.lang.reflect.Method.invoke(Method.java:511) 
03-18 10:41:11.287: E/AndroidRuntime(843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-18 10:41:11.287: E/AndroidRuntime(843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-18 10:41:11.287: E/AndroidRuntime(843): at dalvik.system.NativeStart.main(Native Method) 
03-18 10:41:11.287: E/AndroidRuntime(843): Caused by: java.lang.NullPointerException 
03-18 10:41:11.287: E/AndroidRuntime(843): at com.example.androidhive.CustomizedListViewPackage.onCreate(CustomizedListViewPackage.java:52) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.Activity.performCreate(Activity.java:5104) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
03-18 10:41:11.287: E/AndroidRuntime(843): ... 11 more 

CustomizedListViewPackage.java:

 import java.util.ArrayList; 
import java.util.HashMap; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 


    public class CustomizedListViewPackage extends Activity { 
// All static variables 
static final String URL = "http://erachnida.net/android/packages.xml"; 
// XML node keys 
static final String KEY_SONGS = "songs"; // parent node 
static final String KEY_ID = "id"; 
static final String KEY_TITLE = "title"; 
static final String KEY_OFFER = "offer"; 
static final String KEY_DETAIL = "detail"; 
static final String KEY_CHECKS = "checks"; 
static final String KEY_DRINK = "drink"; 
static final String KEY_SERVICE = "service"; 
static final String KEY_FOOD = "food"; 
static final String KEY_INTERNET = "internet"; 
static final String KEY_RATES = "rates"; 
static final String KEY_DURATION = "duration"; 
static final String KEY_THUMB_URL = "thumb_url"; 

ListView lists; 
LazyAdapterPackage adapters; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_package); 


    final ArrayList<HashMap<String, String>> songsLists = new ArrayList<HashMap<String, String>>(); 

    XMLParser parser = new XMLParser(); 
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
    Document doc = parser.getDomElement(xml); // getting DOM element 
    System.out.println("Getting DOM Element(s):- " + doc); // getting null here 
    // Getting null pointer in below line(line number - 52) 
    NodeList nl = doc.getElementsByTagName(KEY_SONGS); 
    // looping through all song nodes <song> 
    for (int i = 0; i < nl.getLength(); i++) { 
     // creating new HashMap 
     HashMap<String, String> map = new HashMap<String, String>(); 
     Element e = (Element) nl.item(i); 
     // adding each child node to HashMap key => value 
     map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
     map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
     map.put(KEY_OFFER, parser.getValue(e, KEY_OFFER)); 
     map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL)); 
     map.put(KEY_CHECKS, parser.getValue(e, KEY_CHECKS)); 
     map.put(KEY_DRINK, parser.getValue(e, KEY_DRINK)); 
     map.put(KEY_SERVICE, parser.getValue(e, KEY_SERVICE)); 
     map.put(KEY_FOOD, parser.getValue(e, KEY_FOOD)); 
     map.put(KEY_INTERNET, parser.getValue(e, KEY_INTERNET)); 
     map.put(KEY_RATES, parser.getValue(e, KEY_RATES)); 
     map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION)); 
     map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 

     // adding HashList to ArrayList 
     songsLists.add(map); 
    } 


    lists=(ListView)findViewById(R.id.list); 

    // Getting adapter by passing xml data ArrayList 
    adapters=new LazyAdapterPackage(this, songsLists);   
    lists.setAdapter(adapters); 



    // Click event for single list row 
    lists.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      HashMap<String, String> map = songsLists.get(position); 
      Intent in = new Intent(CustomizedListViewPackage.this,com.example.androidhive.SingleViewPackage.class); 
      in.putExtra(KEY_TITLE, map.get(KEY_TITLE)); 
      in.putExtra(KEY_OFFER, map.get(KEY_OFFER)); 
      in.putExtra(KEY_DETAIL, map.get(KEY_DETAIL)); 
      in.putExtra(KEY_CHECKS, map.get(KEY_CHECKS)); 
      in.putExtra(KEY_DRINK, map.get(KEY_DRINK)); 
      in.putExtra(KEY_SERVICE, map.get(KEY_SERVICE)); 
      in.putExtra(KEY_FOOD, map.get(KEY_FOOD)); 
      in.putExtra(KEY_INTERNET, map.get(KEY_INTERNET)); 
      in.putExtra(KEY_RATES, map.get(KEY_RATES)); 
      in.putExtra(KEY_DURATION, map.get(KEY_DURATION)); 
      in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL)); 
      startActivity(in); 


     } 
    });  
} 
} 

請參見下面的代碼XMLParser.java:

public class XMLParser { 

// constructor 
public XMLParser() { 

} 

/** 
* Getting XML from URL making HTTP request 
* @param url string 
* */ 
public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // return XML 
    return xml; 
} 

/** 
* Getting XML DOM element 
* @param XML string 
* */ 
public Document getDomElement(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); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

     return doc; 
} 

/** Getting node value 
    * @param elem element 
    */ 
public final String getElementValue(Node elem) { 
    Node child; 
    if(elem != null){ 
     if (elem.hasChildNodes()){ 
      for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
       if(child.getNodeType() == Node.TEXT_NODE ){ 
        return child.getNodeValue(); 
       } 
      } 
     } 
    } 
    return ""; 
} 

/** 
    * Getting node value 
    * @param Element node 
    * @param key string 
    * */ 
public String getValue(Element item, String str) {  
     NodeList n = item.getElementsByTagName(str);   
     return this.getElementValue(n.item(0)); 
    } 
    } 

在此應用程序使用單XMLParser的班數的ListView類,像CustomizedListView.java和CustomizedListViewPackage.java

+0

您可以發佈您getDomElement(XML)方法代碼.. – Subburaj 2013-03-18 05:25:04

+0

PLZ發表您的完整的日誌有問題 – 2013-03-18 05:32:53

+0

沒喲嘗試打印的XML你從URL獲得的字符串,我你的getXmlFromUrl函數。它是一個格式良好的XML嗎? – abhinav 2013-03-18 05:42:20

回答

0

試試這個

String xml = parser.getXmlFromUrl(URL); 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
Document dom = builder.parse(xml); 
Element root = dom.getDocumentElement(); 
+0

[與XML在Android上工作(http://www.ibm.com/developerworks/opensource/library/x-android/) - 它包括了分析實例的所有選項。 – DroidBoy 2013-03-18 05:43:22

0

如登錄:

獲取DOM元素(S) - 空

意味着ü不從服務器獲取數據。更改getXmlFromUrl方法,使用獲得InputStream然後從它作爲字符串讀取數據:

public String getXmlFromUrl(String url) { 
     String xml = null; 
     InputStream content = null; 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response = httpclient.execute(new HttpGet(url)); 
      content = response.getEntity().getContent(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     // return XML 
     return inputStreamToString(content).toString(); 
    } 

    private StringBuilder inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuilder xml = new StringBuilder(); 

     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     try { 
      while ((line = rd.readLine()) != null) { 
       xml.append(line); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return xml; 
    } 
相關問題