2013-04-08 65 views
0

我想請教一些關於Android編程.. 我創建從RSS XML文件到Android返回顯示器的一類,但我得到了一些錯誤了java.lang.RuntimeException:無法啓動活動ComponentInfo錯誤

04 -08 14:37:19.162:E/AndroidRuntime(381): java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.example.xmlreader/com.example.xmlreader.MainActivity}: java.lang.NullPointerException

代碼:

static final String URL = "http://api.androidhive.info/pizza/?format=xml"; 
// XML node keys 
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_ID = "id"; 
static final String KEY_NAME = "name"; 
static final String KEY_COST = "cost"; 
static final String KEY_DESC = "description"; 

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

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 

    XMLParser parser = new XMLParser(); 
    String xml = parser.getXMLFromURL(URL); // getting XML 
    Document doc = parser.getDomElem(xml); // getting DOM element 

(*) NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
    // looping through all item nodes <item> 
    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_NAME, parser.getValue(e, KEY_NAME)); 
     map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST)); 
     map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); 

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

    // Adding menuItems to ListView 
    ListAdapter adapter = new SimpleAdapter(this, menuItems, 
      R.layout.list_item, 
      new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { 
        R.id.name, R.id.desciption, R.id.cost }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString(); 
      String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), SingleActivity.class); 
      in.putExtra(KEY_NAME, name); 
      in.putExtra(KEY_COST, cost); 
      in.putExtra(KEY_DESC, description); 
      startActivity(in); 

     } 
    }); 
} 

在第38行或行的錯誤我是給(*)這個標誌... 懇求幫助我...

+0

DOC

<uses-permission android:name="android.permission.INTERNET"/>

一個例子是等於'null' – thepoosh 2013-04-08 08:52:05

回答

0

如果空指針就要到了下面行(帶*號):

(*) NodeList nl = doc.getElementsByTagName(KEY_ITEM); //i reckon the nullpointer is coming up on the doc object... 

然後我想檢查哪個正在從getXMLFromURL方法 檢索的XML是否正確?:

XMLParser parser = new XMLParser(); 
String xml = parser.getXMLFromURL(URL); // getting XML (Is this correctly retreived? 
Document doc = parser.getDomElem(xml); // getting DOM element (This is returning null) 

如需更多幫助,分享您XMLParser的類

+0

你的答案聽起來更像是一個評論,因爲你是不是給人一種方式來解決問題。 – Egor 2013-04-08 08:47:04

+0

「然後我估計檢查從getXMLFromURL方法檢索到的xml是否正確?:」(答案是xml不正確) 將所有上述解釋與正確的代碼縮進放在評論? – 2013-04-08 08:51:50

+0

縮短描述以使其適合評論是有意義的,僅僅因爲回答問題意味着提出明確的解決問題的方法,而不是對代碼做出假設,並聲明您沒有足夠的信息給出更精確的解決方案。 – Egor 2013-04-08 08:58:37

0
  1. 你需要使用一個線程的AsyncTask執行「網絡操作」。否則,你會得到NetworkOnMainThreadException。 read this

  2. 爲AndroidManifest.xml添加權限。使用線程

    menuItems = new ArrayList<HashMap<String, String>>(); 
    final XMLParser parser = new XMLParser(); 
    
    Thread th = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         xml = parser.getXmlFromUrl(URL); 
    
         handler.post(new Runnable() { //create an object of Handler class in onCreate() - (android.os.Handler) 
          @Override 
          public void run() { 
           Document doc = parser.getDomElement(xml); 
           nl =doc.getElementsByTagName(KEY_ITEM); 
           //other code inside onCreate() - for loop 
          } 
         }); 
        } 
    } 
    th.start(); 
    
    SimpleAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item, 
          new String[]{KEY_NAME, KEY_COST, KEY_DESCRIPTION}, new int[]{R.id.name, R.id.cost, R.id.description}); 
    
    setListAdapter(adapter); 
    
相關問題