0

谁能帮我弄清楚为什么有时 AsyncTask GeoCoder 有时会工作,而有时会崩溃?我浏览了一些与该主题相关的帖子,但似乎无法与之相关。

这是我的代码:

public class statuspage extends MapActivity {

LocationManager locationManager;
MapView mapView;
Criteria criteria;
Location location;
Geocoder gc;
Address address;

String bestProvider;
String LOCATION_SERVICE = "location";
String addressString = "No address found";
String lonLat = "lonLat";
StringBuilder sb;
StringBuilder ssb;

private MapController mapController;
private MyLocationOverlay myLocation;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statuspage);

    // Get Mapping Controllers etc //
    mapView = (MapView) findViewById(R.id.mapView);
    mapController = mapView.getController();
    mapController.setZoom(17);
    mapView.setBuiltInZoomControls(true);

    // Add the MyLocationOverlay //
    myLocation = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocation);
    myLocation.enableMyLocation();

    // Animates the map to GPS Position //
    myLocation.runOnFirstFix(new Runnable() {
        public void run() {
            mapController.animateTo(myLocation.getMyLocation());
        }
    });
}

@Override
protected boolean isRouteDisplayed() {

    // Executes GeoCoding AsyncTask //
    new GeoCoder().execute();

    // Location Manager Intiation
    locationManager = (LocationManager) statuspage.this
            .getSystemService(LOCATION_SERVICE);
    criteria = new Criteria();

    // More accurate, GPS fix.
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix.
    bestProvider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {

        // Latitude and Longitude TextView
        TextView etlongitude = (TextView) findViewById(R.id.etlongitude);
        TextView etlatitude = (TextView) findViewById(R.id.etlatitude);

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        // Latitude and Longitude TextView Display Coordinates //
        etlongitude.setText((longitude) + "");
        etlatitude.setText((latitude) + "");

        // locationManager.removeUpdates((LocationListener) location);
        locationManager = null;

    } else {

    }

    return false;
}

class GeoCoder extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        Log.d("AsyncTask", "GeoCoder-doInBackGround");

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        gc = new Geocoder(statuspage.this, Locale.getDefault());
        try {

            List<Address> addresses = gc.getFromLocation(latitude,
                    longitude, 1);

            sb = new StringBuilder();
            if (addresses != null && addresses.size() > 0) {
                address = addresses.get(0);
                int noOfMaxAddressLine = address.getMaxAddressLineIndex();
                if (noOfMaxAddressLine > 0) {
                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                        sb.append(address.getAddressLine(i)).append("\n");
                    }
                    addressString = sb.toString();
                }
            }
        } catch (Exception e) {
            addressString = e.toString();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        TextView scrollview = (TextView) findViewById(R.id.scrollview);
        scrollview.setText("Current Position:" + "\n"
                + "(Accurate to 500 meters)" + "\n" + (addressString));

        Log.d("AsyncTask", "GeoCoder-onPostExecute");
        return;
    }

}

@Override
protected void onResume() {
    super.onResume();
    myLocation.enableMyLocation();
    //new GeoCoder().execute();
}

@Override
protected void onPause() {
    super.onPause();
    myLocation.disableMyLocation();
    //new GeoCoder().cancel(true);
}

@Override
public void onBackPressed() {

    // Button OnClick Vibrating Service
    Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    // Vibrate Service
    vib.vibrate(50);

    startActivity(new Intent(statuspage.this, AgentPortalActivity.class));
    statuspage.this.finish();

    /** Fading Transition Effect */
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);

    return;

}
 }

这是我的日志:

 04-18 08:36:42.812: I/Maps.MyLocationOverlay(18136): Request updates from gps
 04-18 08:36:42.812: I/Maps.MyLocationOverlay(18136): Request updates from network
 04-18 08:36:42.859: I/MapActivity(18136): Handling network change notification:CONNECTED
 04-18 08:36:42.859: E/MapActivity(18136): Couldn't get connection factory client
 04-18 08:36:42.910: D/AsyncTask(18136): GeoCoder-doInBackGround
 04-18 08:36:42.972: W/dalvikvm(18136): threadid=25: thread exiting with uncaught exception (group=0x40015578)
 04-18 08:36:42.995: E/AndroidRuntime(18136): FATAL EXCEPTION: AsyncTask #15
 04-18 08:36:42.995: E/AndroidRuntime(18136): java.lang.RuntimeException: An error occured while executing doInBackground()
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at android.os.AsyncTask$3.done(AsyncTask.java:200)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.lang.Thread.run(Thread.java:1019)
 04-18 08:36:42.995: E/AndroidRuntime(18136): Caused by: java.lang.NullPointerException
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at com.jetdelivery.mobile.statuspage$GeoCoder.doInBackground(statuspage.java:115)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at com.jetdelivery.mobile.statuspage$GeoCoder.doInBackground(statuspage.java:1)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
 04-18 08:36:42.995: E/AndroidRuntime(18136):   ... 4 more

多谢你们!

4

3 回答 3

3

从页面Geocoder.getFromLocation

返回地址对象的列表。如果未找到匹配项或没有可用的后端服务,则返回null或空列表。

所以你需要在之前检查null:

if (addresses.size() > 0)
于 2012-04-17T20:42:39.923 回答
0

每次在 geocoder.getFromLocation(lat, lng, 1); 上我都会遇到 (IOException e) 崩溃;然而,我只是打开/关闭我的三星手机,它就可以工作了......

于 2014-09-03T01:28:18.407 回答
-1

试试这个代码

尝试 {

    List<Address> addresses = gc.getFromLocation(latitude,
            longitude, 1);
    sb = new StringBuilder();
        if (addresses!=null && addresses.size() > 0) {
            address = addresses.get(0);
            int noOfMaxAddressLine = address.getMaxAddressLineIndex();
            if(noOfMaxAddressLine > 0){
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                   sb.append(address.getAddressLine(i)).append("\n");
                }
                addressString = sb.toString();
            }
        }
    } catch (Exception e) {
        addressString = e.toString();
    }
于 2012-04-18T03:38:17.837 回答