0

JSONデータをmapviewにフェッチしても、まだマップを取得していますが、JSONファイルで指定したマップの場所がなく、アプリを実行するたびにこれを取得します-残念ながらアプリが停止し、Logcatは次のように言います:-

 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();
}
4

3 に答える 3

1

これを読んだことがありますか?

InAsynctask doinBackground()メソッドは、Asynctask によって作成された別のスレッドで実行されます。これは、実際にはタスクを非同期にして負荷を軽減する目的です。UIThread

一方、onPostExecute()メソッドはの実行UIThread後に実行されますdoInBanckground()

で特定のタスクの実行後に表示する必要がある変更は、たとえば、 で何らかの Web サービス アクションを実行している場合、それを却下して使用している場合は、 でdoInBackground記述する必要があります。onPostExecutedoInBackgroundProgressDialogonPostExecute

または、あなたの場合、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));
               }
           }
              }

    }
于 2012-10-27T06:24:27.793 に答える
1

は、ビューがある GUI にアクセスできない別のスレッド内でAsyncTaskすべてを実行します。doInBackground()

preExecute()postExecute()この新しいスレッドで重労働が発生する前後に GUI へのアクセスを提供し、長い操作の結果を に渡して、処理の結果を表示することもできpostExecute()ます。

ここで私の答えを見てください。

完璧ではありませんが、アイデアを提供してみてください。

@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();
}
于 2012-10-27T06:21:26.020 に答える
0

TapControlledMapView オブジェクトを初期化しませんでした。初期化して試してみてください。

于 2012-10-27T07:40:19.073 に答える