我的应用程序崩溃与gps奇怪的getloation功能

时间:2017-12-13 23:22:00

标签: android gps

应用程序在getlastknown位置崩溃 我不知道为什么它在那里崩溃 我还在主要节目中包含了所有必需的权限 它崩溃了

  

位置location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

public class AddressSelect extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
LocationManager locationManager;
myloclis loc;
String addressdelv="";
static ArrayList<Integer> prices;
static ArrayList<String> quantities;
static ArrayList<String> namesorder;
Button submitorder;
Marker marker;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address_select);
    prices=getIntent().getIntegerArrayListExtra("Prices");
    quantities=getIntent().getStringArrayListExtra("quantityneeded");
    namesorder=getIntent().getStringArrayListExtra("Productsnames");
    submitorder=(Button)findViewById(R.id.submitorder);
    submitorder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(addressdelv.equals("")){
                Toast.makeText(getApplicationContext(),"Please select a valid location",Toast.LENGTH_LONG).show();

            }else{
                Intent i = new Intent(AddressSelect.this,OrderSummary.class).putExtra("Productsnames",namesorder).putExtra("quantityneeded",quantities).putExtra("Prices",prices).putExtra("addressdelv",addressdelv);
                startActivity(i);
            }
        }
    });
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    // Add a marker in Sydney and move the camera
    LatLng cairo = new LatLng(30.0444, 31.2357);
   // mMap.addMarker(new MarkerOptions().position(cairo).title("Marker in Cairo"));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cairo,10));
    getlocation();
    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {


        @Override
        public void onMapClick(LatLng latLng) {
            if (marker != null) {
                marker.remove();
            }

            marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

            Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addressList;
            try{
                addressList=coder.getFromLocation(latLng.latitude,latLng.longitude,1);
                if(!addressList.isEmpty()){
                    addressdelv=addressList.get(0).getAddressLine(0);
                    Toast.makeText(getApplicationContext(),addressdelv,Toast.LENGTH_LONG).show();
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
                }else{
                    Toast.makeText( getApplicationContext(),"We don't deliver to this location sorry",Toast.LENGTH_LONG).show();

                }
            }catch (Exception e){
                Toast.makeText( getApplicationContext(),"Cant get your address check network",Toast.LENGTH_LONG).show();
            }
        }
    });

}


public void getlocation(){
    if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,Manifest.permission.INTERNET)!=PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }else{
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
            LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
            if (marker != null) {
                marker.remove();
            }

            marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

            Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addressList;
            try{
                addressList=coder.getFromLocation(latLng.latitude,latLng.longitude,1);
                if(!addressList.isEmpty()){
                    addressdelv=addressList.get(0).getAddressLine(0);
                    Toast.makeText(getApplicationContext(),addressdelv,Toast.LENGTH_LONG).show();
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
                }else{
                    Toast.makeText( getApplicationContext(),"We don't deliver to this location sorry",Toast.LENGTH_LONG).show();

                }
            }catch (Exception e){
                Toast.makeText( getApplicationContext(),"Cant get your address check network",Toast.LENGTH_LONG).show();
            }
        }else{
           Toast.makeText(getApplicationContext(),"Can't get location sorry",Toast.LENGTH_LONG).show();
        }
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case 1:
            getlocation();
            break;
    }
}

}

1 个答案:

答案 0 :(得分:0)

看起来你永远不会初始化locationManager,所以我猜你在尝试使用它时会得到一个NullPointerException。你应该添加

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

或者,请参阅:Getting the Last Known Location