本机启动在startNavigating android GPS中失败

本机启动在startNavigating android GPS中失败,android,gps,Android,Gps,我是android开发新手。我正在尝试使用android中的GPS获取位置。我采用了两个课程:一个是GP斯特拉克课程,另一个是活动课程。每次都将该位置设置为null,当我签入LogCat时,会出现以下错误: native_start failed in startNavigating 我已经在清单中设置了GPS数据的权限。我甚至试图通过使用模拟器控件来传递假纬度和假经度,但没有效果。原因可能是什么?我正在为Android 2.3编写代码 我的代码是://GPSTracker.java publ

我是android开发新手。我正在尝试使用android中的GPS获取位置。我采用了两个课程:一个是GP斯特拉克课程,另一个是活动课程。每次都将该位置设置为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();
                    }

            }
        });

嘿,朋友,看看这个


我还以为我试图从网络提供商那里获得位置。在将权限和GPSProvider更改为网络提供程序后,我无法删除错误,但即使在从emulator控件传入数据后,仍然将位置设置为Null。完全被这件事缠住了/hey friend private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=0;私有静态最终长最小时间更新=0;您必须保留此0,然后只有您才能在待机状态下获取位置当前代码您将其设置为10,这意味着在移动10米后,它将覆盖onlocationchamge方法****locationManager.removeUpdate(locationListner);*******locationManager.RequestLocationUpdate(locationManager.GPS\u提供程序、最短时间\u BW\u更新、最短距离\u更改\u更新、侦听器);请尝试从网络提供商处获取位置信息,并立即开始工作。仍然不能在模拟器上工作,但可以在我的设备上工作。只有一个问题。纬度和经度会随着位置的变化而自动变化吗?目前,在我的设备上,我使用地图获得了位置。在我移动到另一个位置后,我不得不使用地图本身更改位置。有没有一种方法可以让这个过程自动化?onLocationChange()方法确实建议您这样做……是的,它们会自动更改,但最好的做法是在您准备就绪时注销LocationListeners,否则会耗尽您的电池电量,这当然不是开发应用程序的好做法,所以请尝试在开始时依赖lastknownlocation。当然,这取决于你的应用程序需要多少准确的位置,如果需要的话,那么你必须折衷电池的最佳理想值id MIN_DISTANCE_CHANGE_for_UPDATES=10m n,因为你在印度,所以有90%的几率你会得到空位置,但这样的事情不会发生事实上更新发生在外面请给答案打分