3

[現在の場所を検索] ボタンをクリックすると、「申し訳ありません。強制的に閉じます」と表示されます。緯度と経度を表示したい。私の携帯では GPS がオンになっています。それでもエラーメッセージが表示されます。コードの何が問題になっていますか?

public class CurrentLocation extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;
DBAdapter dbA = new DBAdapter (CurrentLocation.this);

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.curloc);
    Button bacButton = (Button)findViewById(R.id.bk);

    bacButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent explicitIntent = new Intent(CurrentLocation.this,ImageSearchActivity.class);
            startActivity(explicitIntent);
        }
    });

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showCurrentLocation();
        }
    });        

}    

protected void showCurrentLocation() {

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) {
        String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s",
            location.getLongitude(), location.getLatitude());
        double longit = location.getLongitude();
        double latid = location.getLatitude();

        try {
            dbA.open();
            dbA.Insert(longit,latid);
            dbA.close();
        } catch (Exception e) {
            Toast.makeText(CurrentLocation.this,e.getMessage(), Toast.LENGTH_LONG).show();
        }
        Toast.makeText(CurrentLocation.this, message,Toast.LENGTH_LONG).show();
    }
}   

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s",
            location.getLongitude(), location.getLatitude());
        Toast.makeText(CurrentLocation.this, message, Toast.LENGTH_LONG).show();
        Toast.makeText(CurrentLocation.this, "Location Stored Successfully", Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {

        Toast.makeText(CurrentLocation.this, "Provider status changed",
        Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(CurrentLocation.this,"Provider disabled by the user. GPS turned off",
            Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(CurrentLocation.this, "Provider enabled by the user. GPS turned on",
            Toast.LENGTH_LONG).show();
    }
}

} // end of class
4

1 に答える 1

0

関数でこれを呼び出しますGetCurrentLocation

try {
    gps_enabled=lmGps.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {
}

try {
    lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
} catch(Exception ex){
}

このように呼び出すgpsLocationListenerと、現在の場所が返され、それに対してやりたいことは何でも実行されます。

LocationListener gpsLocationListener=new LocationListener(){

    public void onLocationChanged(Location location){
        dGpsLat=location.getLatitude();
        dGpsLng=location.getLongitude();

        dLat=dGpsLat;
        dLng=dGpsLng;

        if(dLat>0 && dLng>0){ (...) }

        try {
            //SaveLocation();
        } catch (Exception e) {
            sResponse=e.toString();
            return;
        }
    }

    public void onStatusChanged(String provider,int status,Bundle extras){}
    public void onProviderEnabled(String provider){}
    public void onProviderDisabled(String provider){}

};
于 2012-10-18T07:21:11.530 に答える