2017-12-13 9 views
0

アプリのクラッシュと私のアプリがクラッシュ私はそれがそこに をクラッシュする理由を知らないにも私はそれがgetlastknown場所 のGPS奇妙なgetloationfunction

場所場所= 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; 
    } 
} 

} 

答えて

0

あなたがlocationManagerを初期化することはありませんように見えますので、私はそれを使用しようとしたときにNullPointerExceptionが取得推測しています。あなたは、また

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

を追加表示されるはずです。Getting the Last Known Location