我试图从一个片段中获取GPS坐标。我没有收到任何错误,但应用程序在启动时崩溃

时间:2016-05-02 12:05:53

标签: android eclipse android-fragments gps

我认为我的Location Listener存在问题,但我无法确定问题。当我删除代码以获得GPS时,我的应用程序运行正常。

public class RescueFragment extends Fragment{

    public RescueFragment(){}
    private static final long mindisch=1;
    private static final long mintim=1000;
    protected LocationManager locationManager;
    protected Double latitude,longitude;

    TextView alert_display;
    Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_rescue, container, false);
        alert_display = (TextView)rootView.findViewById(R.id.alert_display);       
        btn = (Button) rootView.findViewById(R.id.button_rescue_me);

        locationManager =(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,mindisch,mintim, new MyLocationListener()   );

        btn.setOnClickListener(new OnClickListener() {  

            @Override
            public void onClick(View arg0) {


                alert_display.setText("Alert Sent to User");
                showCurrentLocation();
            }
        });

        return rootView;
    }

    protected void showCurrentLocation()
    {
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location!=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
            Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show();
        }
    }

    private class MyLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
            Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getActivity(), "Provider Status Changed: ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getActivity(), "GPS ON", Toast.LENGTH_LONG).show();          
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getActivity(), "GPS OFF:", Toast.LENGTH_LONG).show();            
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在您的清单中添加此Permissions

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

对于Android Versions 6.0,您需要允许用户提供Runtime Permissions

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
        PackageManager.PERMISSION_GRANTED &&
        ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
        PackageManager.PERMISSION_GRANTED) {
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
    Toast.makeText(this, R.string.error_permission_map, Toast.LENGTH_LONG).show();
}