2012-10-27 141 views
0

獲取JSON數據到mapview中,我仍然獲取地圖,但沒有在JSON文件中給出的Map Locations,並且每當我運行我的應用程序時,不幸的是應用程序已經停止,logcat的說: -引起java.lang.NullPointerException,不幸的是應用程序已停止

10-27 13:02:18.573: E/AndroidRuntime(719): Caused by: java.lang.NullPointerException 
10-27 13:02:18.573: E/AndroidRuntime(719):  at com.erachnida.restaurant.versionoct.MapView.onCreate(MapView.java:52) 

請參閱下面的代碼: -

public class MapView extends MapActivity { 
public GeoPoint point; 
TapControlledMapView mapView; // use the custom TapControlledMapView 
List<Overlay> mapOverlays; 
Drawable drawable; 
SimpleItemizedOverlay itemizedOverlay; 
JSONObject json = null; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    mapOverlays = mapView.getOverlays(); 
    drawable = getResources().getDrawable(R.drawable.ic_launcher); 
    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView); 
    itemizedOverlay.setShowClose(false); 
    itemizedOverlay.setShowDisclosure(true); 
    itemizedOverlay.setSnapToCenter(false); 
    new AsyncTask<Void, Void, Void>() { 



     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      HttpClient client = new DefaultHttpClient(); 
      // Perform a GET request for a JSON list 
      HttpUriRequest request = new HttpGet(
        "https://dl.***.com/maps.json"); 
      // Get the response that sends back 
      HttpResponse response = null; 
      try { 
       response = client.execute(request); 
      } catch (ClientProtocolException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      // Convert this response into a readable string 
      String jsonString = null; 
      try { 
       jsonString = StreamUtils.convertToString(response 
         .getEntity().getContent()); 
      } catch (IllegalStateException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // Create a JSON object that we can use from the String 
      JSONObject json = null; 
      try { 
       json = new JSONObject(jsonString); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 


      try { 

       JSONArray jsonArray = json.getJSONArray("maps"); 
       Log.e("log_tag", "Opening JSON Array "); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String latitude = jsonObject.getString("latitude"); 
        String longitude = jsonObject.getString("longitude"); 
        String title = jsonObject.getString("title"); 
        String country = jsonObject.getString("country"); 
        double lat = Double.parseDouble(latitude); 
        double lng = Double.parseDouble(longitude); 
        Log.e("log_tag", "ADDING GEOPOINT" + title); 
        point = new GeoPoint((int) (lat * 1E6), 
          (int) (lng * 1E6)); 
        OverlayItem overlayItem = new OverlayItem(point, title, 
          country); 
        itemizedOverlay.addOverlay(overlayItem); 
       } 
      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 

      itemizedOverlay.populateNow(); 

      mapOverlays.add(itemizedOverlay); 
      if (savedInstanceState == null) { 
       MapController controller = mapView.getController(); 
       controller.setCenter(point); 
       controller.setZoom(7); 
      } else { 
       // example restoring focused state of overlays 
       int focused; 
       focused = savedInstanceState.getInt("focused_1", -1); 
       if (focused >= 0) { 
        itemizedOverlay.setFocus(itemizedOverlay 
          .getItem(focused)); 
       } 
      } 

     } 

    }.execute(); 
} 

回答

1

AsyncTask執行一切doInBackground()另一個線程,它沒有進入GUI,其中的內你的觀點是。訪問前,在這個新的線程中發生繁重後GUI

preExecute()postExecute()的報價,你可以長時間操作的結果,甚至傳遞到postExecute()然後顯示處理的任何結果。

Here看到我的答案。

嘗試跟隨它的不完美,但給你的想法。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    mapOverlays = mapView.getOverlays(); 
    drawable = getResources().getDrawable(R.drawable.ic_launcher); 
    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView); 
    itemizedOverlay.setShowClose(false); 
    itemizedOverlay.setShowDisclosure(true); 
    itemizedOverlay.setSnapToCenter(false); 
    new AsyncTask<Void, Void, Void>() { 



     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      HttpClient client = new DefaultHttpClient(); 
      // Perform a GET request for a JSON list 
      HttpUriRequest request = new HttpGet(
        "https://dl.***.com/maps.json"); 
      // Get the response that sends back 
      HttpResponse response = null; 
      try { 
       response = client.execute(request); 
      } catch (ClientProtocolException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      // Convert this response into a readable string 
      String jsonString = null; 
      try { 
       jsonString = StreamUtils.convertToString(response 
         .getEntity().getContent()); 
      } catch (IllegalStateException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // Create a JSON object that we can use from the String 
      JSONObject json = null; 
      try { 
       json = new JSONObject(jsonString); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 


      try { 
       JSONArray jsonArray = json.getJSONArray("maps"); 
       Log.e("log_tag", "Opening JSON Array "); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String latitude = jsonObject.getString("latitude"); 
        String longitude = jsonObject.getString("longitude"); 
        String title = jsonObject.getString("title"); 
        String country = jsonObject.getString("country"); 
        double lat = Double.parseDouble(latitude); 
        double lng = Double.parseDouble(longitude); 
        Log.e("log_tag", "ADDING GEOPOINT" + title); 
        point = new GeoPoint((int) (lat * 1E6), 
          (int) (lng * 1E6)); 
        OverlayItem overlayItem = new OverlayItem(point, title, 
          country); 
        itemizedOverlay.addOverlay(overlayItem); 
       } 
      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 

      itemizedOverlay.populateNow(); 

      mapOverlays.add(itemizedOverlay); 
      if (savedInstanceState == null) { 
       MapController controller = mapView.getController(); 
       controller.setCenter(point); 
       controller.setZoom(7); 
      } else { 
       // example restoring focused state of overlays 
       int focused; 
       focused = savedInstanceState.getInt("focused_1", -1); 
       if (focused >= 0) { 
        itemizedOverlay.setFocus(itemizedOverlay 
          .getItem(focused)); 
       } 
      } 

     } 

    }.execute(); 
} 
+0

hardik謝謝哥們,請你把我的代碼放在我身上,這樣對我來說會更容易理解 – Stanley

+0

你首先參考http://www.androidhive.info/2012/01/android-json-parsing -tutorial/this。如果有任何問題,請告訴我。它是你的問題的一個很好的例子。 –

+0

我已經使用相同的代碼來使用json解析器,但我很努力地知道需要在onPostExecute()方法中移動哪些代碼,以及在哪裏放置onPostExecute()方法,請告訴我bro – Stanley

1

你看過this嗎?

AsynctaskdoinBackground()方法執行中通過的AsyncTask,這實際上是使任務異步,以減少對UIThread

然而,onPostExecute()方法做的doInBanckground()

執行之後執行在 UIThread負載的目的而創建單獨的線程

doInBackground中執行特定任務後需要看到的更改必須寫入onPostExecute,例如,如果您正在使用doInBackground中的某個webservice操作,您可能會使用ProgressDialog解僱它寫在onPostExecute

或者你的情況你有json數組顯示數值必須寫在onPostExecute裏面。

編輯 試試這個代碼,因爲我不知道mapview剛纔試了一下..

class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 

      HttpClient client = new DefaultHttpClient(); 
      // Perform a GET request for a JSON list 
      HttpUriRequest request = new HttpGet("https://dl.***.com/maps.json"); 
      // Get the response that sends back 
      HttpResponse response = null; 
      try { 
       response = client.execute(request); 
      } catch (ClientProtocolException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      // Convert this response into a readable string 
      String jsonString = null; 
      try { 
       jsonString = StreamUtils.convertToString(response.getEntity().getContent()); 
      } catch (IllegalStateException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // Create a JSON object that we can use from the String 
      JSONObject json = null; 
      try { 
       json = new JSONObject(jsonString); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 


      try{ 
       JSONArray jsonArray = json.getJSONArray("maps"); 
       Log.e("log_tag", "Opening JSON Array "); 
       for(int i=0;i < jsonArray.length();i++){      
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String latitude = jsonObject.getString("latitude"); 
        String longitude = jsonObject.getString("longitude"); 
        String title = jsonObject.getString("title"); 
        String country = jsonObject.getString("country"); 
        double lat = Double.parseDouble(latitude); 
        double lng = Double.parseDouble(longitude); 
        Log.e("log_tag", "ADDING GEOPOINT"+title); 
        point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
        OverlayItem overlayItem = new OverlayItem(point, title, country); 
        itemizedOverlay.addOverlay(overlayItem); 
       } 
      }catch(JSONException e)  { 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 


      return jsonString; 
     } 

      @Override 
     protected void onPostExecute(String result) { 
itemizedOverlay.populateNow(); 

      mapOverlays.add(itemizedOverlay); 
      if (savedInstanceState == null) { 
       MapController controller = mapView.getController(); 
       controller.setCenter(point); 
       controller.setZoom(7); 
      } else { 
       // example restoring focused state of overlays 
       int focused; 
       focused = savedInstanceState.getInt("focused_1", -1); 
       if (focused >= 0) { 
        itemizedOverlay.setFocus(itemizedOverlay.getItem(focused)); 
       } 
      } 
       } 

    } 
+0

請告訴我什麼代碼正是我需要放置在onPostExecute,我之前嘗試過,但得到很多錯誤的原因,這就是爲什麼我也想知道我在做什麼愚蠢的錯誤 – Stanley

+0

需要添加的東西從'json'數組獲取的映射需要在'onPostExecute'方法內。 – Zombie

+0

bobbe我說過我試圖放置,但得到問題,所以你可以請示範我使用我的代碼和你thik onPostExecute()應該看起來像,然後我會找出我失蹤或做錯的地方,所以只需要通過複製和粘貼使用我的代碼來顯示這些行 – Stanley

0

你不初始化TapControlledMapView對象。初始化它並嘗試。

相關問題