簡體   English   中英

地圖未在Android應用中顯示

[英]Map not showing in android app

我有一個使用谷歌mapview的應用程序。 代碼在我的舊設備android 2.1上工作正常但在我的htc一個x運行android 4我不得不異步它以避免網絡主線程異常。

mapview顯示敵人大約一秒然后崩潰。 該錯誤包含一個異常,該異常引用一個不調用Looper.prepare()的線程。 任何想法我怎么能解決它謝謝。

這是例外,我將在下面發布源代碼。

11-14 13:29:31.025: W/dalvikvm(20404): threadid=15: thread exiting with uncaught exception (group=0x40a7d228)
11-14 13:29:31.025: E/AndroidRuntime(20404): FATAL EXCEPTION: AsyncTask #4
11-14 13:29:31.025: E/AndroidRuntime(20404): java.lang.RuntimeException: An error occured while executing doInBackground()
11-14 13:29:31.025: E/AndroidRuntime(20404):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.lang.Thread.run(Thread.java:864)
11-14 13:29:31.025: E/AndroidRuntime(20404): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-14 13:29:31.025: E/AndroidRuntime(20404):    at android.os.Handler.<init>(Handler.java:121)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at android.widget.ZoomButtonsController$2.<init>(ZoomButtonsController.java:170)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at android.widget.ZoomButtonsController.<init>(ZoomButtonsController.java:170)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at com.google.android.maps.MapView.createZoomButtonsController(MapView.java:1444)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at com.google.android.maps.MapView.setBuiltInZoomControls(MapView.java:1498)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at com.carefreegroup.GetClientDirections.getRoute(GetClientDirections.java:173)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at com.carefreegroup.GetClientDirections$AsyncGetRoute.doInBackground(GetClientDirections.java:87)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at com.carefreegroup.GetClientDirections$AsyncGetRoute.doInBackground(GetClientDirections.java:1)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-14 13:29:31.025: E/AndroidRuntime(20404):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

public class GetClientDirections extends MapActivity {

    private MapController mapController;
    private MapView mapView;

    private List<Overlay> mapOverlays;
    private StringBuffer response = null;
    private static final String TAG = GetClientDirections.class.getSimpleName();
    private double lon;
    private double lat;
    private JSONArray routes = null;
    private JSONObject bounds = null;
    private JSONObject northeast = null;
    private JSONObject anonObject;
    private JSONObject overViewPolyline;
    private String stringUrl;
    private String polyPoints;
    Context context;
    private String endAddr;
    private String startAddr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);

        context = this;
        startAddr = "wf120lj";
        endAddr = "wf27ar";



                StringBuilder sb = new StringBuilder();
                sb.append("http://maps.google.com/maps/api/directions/json?origin=");
                sb.append(startAddr);
                sb.append("&destination=");
                sb.append(endAddr);
                sb.append("&sensor=false");

                stringUrl = sb.toString();
                Log.e(TAG, "url = " + stringUrl);


                AsyncGetRoute agr = new AsyncGetRoute();
                agr.execute();




    }// end of onCreate


    private class AsyncGetRoute extends AsyncTask<Void, Void, Void> {



        @Override
        protected Void doInBackground(Void... params) {

            getRoute();
            return null;
        }


    }





    public void getRoute() {

        response = new StringBuffer();
        URL url = null;
        try {
            url = new URL(stringUrl);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpURLConnection httpconn = null;
        try {
            httpconn = (HttpURLConnection) url.openConnection();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // Log.e(TAG,"response code OK ");
                BufferedReader input = new BufferedReader(
                        new InputStreamReader(httpconn.getInputStream()), 8192);
                String strLine = null;

                while ((strLine = input.readLine()) != null) {
                    // Log.e(TAG,""+strLine);
                    response.append(strLine);
                }
                input.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String jsonOutput = response.toString();
        Log.e(TAG, "jsonOutput = " + jsonOutput);

        JSONObject results = null;
        try {

            results = new JSONObject(jsonOutput);

            routes = results.getJSONArray("routes");

            anonObject = routes.getJSONObject(0);
            bounds = anonObject.getJSONObject("bounds");
            overViewPolyline = anonObject.getJSONObject("overview_polyline");
            polyPoints = overViewPolyline.getString("points");
            Log.e(TAG, "overview_polyline  = " + overViewPolyline);
            Log.e(TAG, "points  = " + polyPoints);

            northeast = bounds.getJSONObject("northeast");

            lat = (Double) northeast.get("lat");

            lon = (Double) northeast.get("lng");

            Log.e(TAG, "lon/lat = " + lon + " " + lat);

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        List<GeoPoint> list = decodePoly(polyPoints);



        mapView = (MapView) findViewById(R.id.cfmapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setEnabled(true);
        mapView.setSatellite(true);
        mapController = mapView.getController();
        mapController.setZoom(10);

        mapOverlays = mapView.getOverlays();

        mapOverlays.add(new RoutePathOverlay(list, getApplicationContext()));
        mapController.animateTo(new GeoPoint(list.get(0).getLatitudeE6(), list
                .get(0).getLongitudeE6()));

        mapView.invalidate();

    }// end of getRoute




    private List<GeoPoint> decodePoly(String encoded) {

        List<GeoPoint> poly = new ArrayList<GeoPoint>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),
                    (int) (((double) lng / 1E5) * 1E6));
            poly.add(p);
        }

        return poly;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

你不應該在doInBackground()更新視圖,你需要覆蓋onPostExecute()並在那里更新你的MapView。 getRoute()方法拆分為兩個。

您在非ui線程中使用getRoute()方法。 應該在UI線程中更新MapView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM