2014-10-01 183 views
-1

的Hashmap值當我創建這個對象,我這樣做:訪問數組列表中

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

try { 
    // Getting Array of fields 
    JSONArray fields= json.getJSONArray("fields"); 

    // Looping through All fields 
    for(int i = 0; i < fields.length(); i++) { 
     JSONObject jobj = fields.getJSONObject(i); 

     // Creating new HashMap 
     HashMap<String, String> map_aux = new HashMap<String, String>(); 

     // Adding each child node to HashMap key => value 
     map_aux.put("field_id", jobj.getString("field_id")); 

     map_aux.put("field_text", jobj.getString("field_text")); 
    }  
    // Adding HashList to ArrayList 
    random_fields.add(map_aux); 
} 

,然後當我嘗試進入的每一個領域中包含HashMap的ArrayList中......我喜歡這:

例如:

editText1.setText(random_fields.get(0).get("field_text")); 

,它給了我一個Java空指針錯誤......但如果我這樣做:

System.out.println(random_challenges.get(0).get("field_text")); 

它的工作原理和它打印在我的調試控制檯..正確的字段..哪個可以解決方案?

+0

請花更多精力來設置您的帖子格式。你應該已經能夠從預覽中看到你的代碼是*英里*在右邊,無故滾動。此外,請使用空格而不是製表符在Stack Overflow上進行縮進。 – 2014-10-01 13:36:24

+0

你用'random_fields'設置文本但打印'random_challenges'? – 2014-10-01 13:38:25

+0

確保你的程序不會拋出異常。如果拋出,可能是'random_fields.get(0).get(「field_text」)= null' – 2014-10-01 13:43:46

回答

0

您設置editText的代碼看起來正確。我在這裏可以發現的唯一區別是你在你的System.out.println方法中使用了random_challenges,在你的setText方法中使用了random_fields。

0

你想提供一些實際的代碼文件(不只是片段),我可以看得更近,但從你所提供的,我敢冒險猜測發生了什麼是你認爲你正在填充你的值散列映射(在for循環中)超出範圍,並被清除,留下NULL參考。

您控制檯,因爲它持續存在,保持數據實例化足夠長的時間讓您調試打印它。