2016-05-17 11 views
0

I Location Code bin mit dem Breiten- und Längengrad der Geräte bekommen seine feinen auf folgenden Geräten arbeiten: HTC One M8 Moto X 2ndG NexusP One Plus 2getLatitude und getLongitude Absturz auf Samsung Galaxy S6

Aber es stürzt in Samsung Galaxy S6

Hier ist der Standortcode, den ich verwende.

 public static ParseGeoPoint geoPointFromLocation(Location loc) { 
      return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude()); 
     } 
    myLoc = (MainActivity.currentLocation == null) ? MainActivity.lastLocation : MainActivity.currentLocation; 

around.setLocation(MainActivity.geoPointFromLocation(myLoc)); 

Logcat Hinzugefügt:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
                    at io.wyntr.peepster.Activities.MainActivity.geoPointFromLocation(MainActivity.java:399) 
                    at io.wyntr.peepster.Activities.RecipientsActivity$2.onClick(RecipientsActivity.java:124) 
                    at android.view.View.performClick(View.java:5697) 
                    at android.view.View$PerformClick.run(View.java:22526) 
                    at android.os.Handler.handleCallback(Handler.java:739) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:158) 
                    at android.app.ActivityThread.main(ActivityThread.java:7229) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Location Code im MainActivity verwendet:

boolean hasSetUpInitialLocation; 

    public static Location lastLocation; 
    public static Location currentLocation; 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
    private static GoogleApiClient GoogleApiClient; 
    // Milliseconds per second 
    private static final int MILLISECONDS_PER_SECOND = 1000; 

    // The update interval 
    private static final int UPDATE_INTERVAL_IN_SECONDS = 5; 

    // A fast interval ceiling 
    private static final int FAST_CEILING_IN_SECONDS = 1; 

    // Update interval in milliseconds 
    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = MILLISECONDS_PER_SECOND 
      * UPDATE_INTERVAL_IN_SECONDS; 

    // A fast ceiling of update intervals, used when the app is visible 
    private static final long FAST_INTERVAL_CEILING_IN_MILLISECONDS = MILLISECONDS_PER_SECOND 
      * FAST_CEILING_IN_SECONDS; 
    ParseUser user = ParseUser.getCurrentUser(); 
    // A request to connect to Location Services 
    private LocationRequest locationRequest; 

@TargetApi(Build.VERSION_CODES.KITKAT) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     locationRequest = LocationRequest.create(); 
     coordinatorLayout = (CoordinatorLayout) findViewById(R.id 
       .coordinatorLayout); 

     if (AppStatus.getInstance(this).isOnline()) { 

     } else { 

      TSnackbar snackbar = TSnackbar.make(coordinatorLayout,"No Internet Connection",TSnackbar.LENGTH_LONG); 
      snackbar.show(); 
     } 

     // Set the update interval 
     locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 

     // Use high accuracy 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

     // Set the interval ceiling to one minute 
     locationRequest.setFastestInterval(FAST_INTERVAL_CEILING_IN_MILLISECONDS); 

     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      requestLocationPermissions(); 
     } 

if (checkPlayServices()) { 
      buildGoogleApiClient(); 
     } 

@Override 
    public void onConnected(Bundle bundle) { 
     currentLocation = getLocation(); 
     try{startPeriodicUpdates();} catch (Exception e){ 
      requestLocationPermissions(); 
     } 

    } 

private boolean servicesConnected() { 
     // Check that Google Play services is available 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

     // If Google Play services is available 
     if (ConnectionResult.SUCCESS == resultCode) { 
      return true; 
      // Google Play services was not available for some reason 
     } else { 
      // Display an error dialog 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0); 
      if (dialog != null) { 
       ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
       errorFragment.setDialog(dialog); 
       errorFragment.show(getSupportFragmentManager(), "Wyntr-Beta"); 
      } 
      return false; 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     snackbar = TSnackbar 
       .make(coordinatorLayout,"Connection Suspended",TSnackbar.LENGTH_SHORT); 
     snackbar.show(); 
    } 


    private void startPeriodicUpdates() { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      requestLocationPermissions(); 
     } 
     try{LocationServices.FusedLocationApi.requestLocationUpdates(
       GoogleApiClient, locationRequest, this);} catch(Exception e){ 
      requestLocationPermissions(); 
     } 
    } 

    private Location getLocation() { 
     // If Google Play Services is available 
     if (servicesConnected()) { 
      // Get the current location 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       requestLocationPermissions(); 

      } 
      return LocationServices.FusedLocationApi.getLastLocation(GoogleApiClient); 

     } else { 
      return null; 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     // Connect to the location services client 
     GoogleApiClient.connect(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     GoogleApiClient.disconnect(); 
    } 

    private boolean checkPlayServices() { 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
       GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } else { 
       snackbar = TSnackbar 
         .make(coordinatorLayout, "Your Android Device is not Supported", TSnackbar.LENGTH_SHORT); 
       snackbar.show(); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    protected synchronized void buildGoogleApiClient() { 
     GoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API).build(); 
    } 

    public static ParseGeoPoint geoPointFromLocation(Location loc) { 
     return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude()); 
    } 

Ich habe den Standortcode. Die PlayServices, die ich verwende, wurden deaktiviert, funktionieren aber auf den anderen Geräten. Es funktioniert nicht nur auf den Samsung Galaxy Geräten.

+1

Wie erhalten Sie den Standort? Könnten Sie mehr Code zur Verfügung stellen ... Ihr Standort-Loc-Objekt ist möglicherweise null. – Seishin

+0

Was ist die Android-Version für Samsung Galaxy S6? –

+1

Posten Sie Ihren Logcat! – Rami

Antwort

0

Wenn Ihr Samsung S6 zu Android Eibisch aktualisiert wurde 6, also denke ich, Ihr Problem Erlaubnis Problem wäre, wär 'für Android 6 für die Erlaubnis, in der Laufzeit zu fragen Standort zu ermöglichen.

Bitte überprüfen https://developer.android.com/training/permissions/requesting.html dieses für wie https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous wäre hilfreich für die Erlaubnis, auf der Laufzeit

Dieser Link zu bitten, die neue Art von Berechtigungen zu kennen.

+0

Ich habe die Laufzeitberechtigung akzeptiert und alle oben aufgeführten Geräte haben eine Laufzeit von 6.0 – Savita

Verwandte Themen