2015-06-11 105 views
0

我正在構建一個Android應用程序,我在其中下載包含要在GoogleMap上顯示的內容的XML文件。信息窗口是可點擊的,所以區分不同的窗口我使用Hashmap。無法訪問散列圖

無論何時地圖加載它崩潰與NullPointerException。 HashMap的被命名爲markerEntryHashMap,這裏的一些代碼:

private class DownloadXmlTask extends AsyncTask<String, Void, String> { 

    /** 
    * Hashmap with marker; String ids as key, entry obj as value 
    */ 
    private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap; 

    @Override 
    protected String doInBackground(String... urls) { 
     [...] 
    } 

    @Override 
    protected void onPostExecute(String result) { 

     for (CurvitVacatureXmlParser.Entry entry : mapEntries) { 

      [...] 

      Log.d(TAG, "Marker ID: " + m.getId()); // OK; Marker ID: m0 
      Log.d(TAG, "Entry ID: " + entry.id); // OK; Entry ID: 12 

      // there is no unique identifier in an info-window so I thought using the hashmap to differentiate later on in an OnInfoWindowClickListener 
      // This crashes: FATAL EXCEPTION: main java.lang.NullPointerException 
      markerEntryHashMap.put(m.getId(), entry); 
     } 

    } 
} 

任何你們/加侖發現錯誤了嗎?

+0

你可以發佈你的空指針棧跟蹤 – Babel

+2

你初始化了你的地圖嗎? 'markerEntryHashMap = new HashMap <>();' –

回答

3

你必須初始化你的HashMap

變化

private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap; 

private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap = new HashMap<>(); 
+0

就是這樣,現在完美了!在10分鐘內會給你你的業力... – Grrrben

1

您還沒有初始化一個HashMap,您可以在 onPreExecute或onPostExecute之前初始化它循環如下:

private class DownloadXmlTask extends AsyncTask<String, Void, String> { 

    /** 
    * Hashmap with marker; String ids as key, entry obj as value 
    */ 
    private HashMap<String, CurvitVacatureXmlParser.Entry> markerEntryHashMap; 

    @Override 
    protected String doInBackground(String... urls) { 
     [...] 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     markerEntryHashMap=new HashMap<String,CurvitVacatureXmlParser.Entry>(); 
     for (CurvitVacatureXmlParser.Entry entry : mapEntries) { 

      [...] 

      Log.d(TAG, "Marker ID: " + m.getId()); // OK; Marker ID: m0 
      Log.d(TAG, "Entry ID: " + entry.id); // OK; Entry ID: 12 

      // there is no unique identifier in an info-window so I thought using the hashmap to differentiate later on in an OnInfoWindowClickListener 
      // This crashes: FATAL EXCEPTION: main java.lang.NullPointerException 
      markerEntryHashMap.put(m.getId(), entry); 
     } 

    } 
}