Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AsyncTask地理编码器有时会崩溃?_Android_Android Asynctask - Fatal编程技术网

Android AsyncTask地理编码器有时会崩溃?

Android AsyncTask地理编码器有时会崩溃?,android,android-asynctask,Android,Android Asynctask,有人能帮我弄清楚为什么AsyncTask GeoCoder有时工作,有时崩溃吗?我已经浏览了一些与这个主题相关的帖子,但似乎我与之无关 这里是迈科德: public class statuspage extends MapActivity { LocationManager locationManager; MapView mapView; Criteria criteria; Location location; Geocoder gc; Address address; String b

有人能帮我弄清楚为什么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;

}
 }
谢谢大家

从页面

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

因此,您需要在执行以下操作之前检查null:

if (addresses.size() > 0)
试试这个代码

试一试{

List addresses=gc.getFromLocation(纬度,
经度,1);
sb=新的StringBuilder();
if(addresses!=null&&addresses.size()>0){
地址=地址。获取(0);
int noOfMaxAddressLine=address.getMaxAddressLineIndex();
如果(noOfMaxAddressLine>0){
对于(int i=0;i
我每次在geocoder.getFromLocation(lat,lng,1)上都会遇到(IOE异常)崩溃;
然而,我只是简单地打开/关闭我的三星手机,它就工作了…

如果它确实返回空值,那么他就必须检查地址=零拳,否则他无论如何都会撞车我不太清楚发生了什么。所以我必须检查getFromLocation!=null?如果addresses==null,则跳过引用它的代码t的其余部分。我已经用toast消息设置了if addresses==null。还是偶尔撞车。我是不是应该安排点别的事?对不起,萨尼什回信太晚了。我已经尝试了你向我建议的代码,但它仍然是一样的=/你能给出新的logcat输出吗?谢谢你的快速回复!我真的很感谢你的帮助。这是我的新日志。你能在statuspage.java中发布第115行吗?让我们看看
if (addresses.size() > 0)
    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();
    }