我有一个使用谷歌地图视图的应用程序。该代码在我的旧设备 android 2.1 上运行良好,但在我的 htc one x 运行 android 4 上,我不得不对其进行异步以避免主线程上的网络异常。
地图视图显示敌人大约一秒钟,然后它就崩溃了。该错误包含一个异常,该异常引用了未调用 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;
    }
}