0

Googleマップビューを使用するアプリがあります。コードは古いデバイス Android 2.1 で正常に動作しますが、Android 4 を実行している htc one x では、メイン スレッドの例外でネットワークを回避するために非同期にする必要がありました。

マップビューに約 1 秒後に敵が表示され、その後クラッシュします。エラーには、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;
    }
}
4

2 に答える 2

2

のビューを更新しないでください。そこで MapViewdoInBackground()をオーバーライドして更新する必要があります。メソッドを 2 つにonPostExecute()分割します。getRoute()

于 2012-11-14T14:40:26.933 に答える
0

非 UI スレッドで getRoute() メソッドを使用しています。MapView は UI スレッドで更新する必要があります。

于 2012-11-14T14:40:16.057 に答える