0

私はアンドロイド開発が初めてです。AndroidでGPSを使用して場所を取得しようとしています。私は 2 つのクラスを採用しました。1 つは GPSTracker 用で、もう 1 つはアクティビティ用です。場所は毎回 null として設定されており、LogCat をチェックインすると、次のエラーが表示されます。

native_start failed in startNavigating

GPSデータのマニフェストにも許可を設定しました。エミュレータ コントロールを使用して偽の緯度と経度を渡そうとしましたが、効果がありませんでした。何が理由でしょうか?私はAndroid 2.3用にコーディングしています

私のコードは: //GPSTracker.java

public class GPSTracker extends Service implements LocationListener {

    private final Context mcontext;
    boolean isGPSEnabled = false;
    boolean canGetLocation = false;
    double latitude,longitude;
    Location location;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

    protected LocationManager locationManager;
    protected LocationListener listener;

    public GPSTracker(Context context){
        this.mcontext = context;
        getLocation();
    }

    public Location getLocation() {
        try{
            locationManager = (LocationManager) mcontext.getSystemService(LOCATION_SERVICE);


            listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location1) {
                    // TODO Auto-generated method stub
                    location = location1;
                }
            };

            isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
            if(isGPSEnabled){
                this.canGetLocation = true;
                locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
                Log.d("GPS", "Enabled");
                if(locationManager!=null){
                    location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
                    if(location!=null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        //Log.d("Latitude", latitude.toString());
                        //Log.d("Longitude", longitude.toString());
                    }
                    else{
                        Log.d("Location","Null");
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return location;
        // TODO Auto-generated method stub

    }

    public boolean canGetLocation(){
        return this.canGetLocation;
    }

    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
        alertDialog.setTitle("GPS Settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mcontext.startActivity(intent);
            }
        });
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    public double getLatitude() {
        // TODO Auto-generated method stub
        if(location!=null){
            Log.d("Location",location.toString());
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        // TODO Auto-generated method stub
        if(location!=null){
            Log.d("Location",location.toString());
            longitude = location.getLongitude();
        }
        return longitude;
    }
    }

//LocationActivity.java

public class LocationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);



        Button button = (Button) findViewById(R.id.button1); 
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                  GPSTracker gps = new GPSTracker(LocationActivity.this);
                    if(gps.canGetLocation() == false){
                        Log.d("GPSLogFromActivity","False");
                        gps.showSettingsAlert();
                    }
                    else{
                        double latitude = gps.getLatitude();
                        System.out.println("Latitude: "+latitude);
                        double longitude = gps.getLongitude();
                        System.out.println("Longitud: e"+longitude);

                        Log.d("GPSLogFromActivity","True");
                        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    }

            }
        });
4

1 に答える 1

0

ちょっと友達これを見て

現在地は常に null を返します。どうすればこれを修正できますか?

于 2013-03-21T06:25:43.140 に答える