2017-03-29 3 views
0

Vor allem Entschuldigung für diese dumme Frage zu stellen. Da ich neu bei Android bin, versuche ich eine App zu machen, in der zuerst ein Dialogfeld geöffnet wird, in dem die Erlaubnis gefragt wird, das GPS wie Google Maps oder Ola zu aktivieren. Danach wollte ich den Breiten- und Längengrad des Benutzers bekommen, aber leider habe ich so viele Links gesucht und gesucht, aber ich habe keine richtige Lösung gefunden. also kann mir jemand helfen.IllegalArgumentException: GoogleApiClient-Parameter ist erforderlich

mein Code:

public class GpsPlaceSearch extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener 
{ 
    Activity context; 
    GoogleApiClient googleApiClient, getGoogleApiClient; 
    protected static final int REQUEST_CHECK_SETTINGS = 0x1; 
    LocationRequest locationRequest1; 

    Location location; 
    private double currentLatitude; 
    private double currentLongitude; 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111; 

    public GpsPlaceSearch(){ 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gps_place_search); 
     getGoogleApiClient = new GoogleApiClient.Builder(this) 
       class will handle connection stuff 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 

       .addApi(LocationServices.API) 
       .build(); 

     locationRequest1=locationRequests(); 
    } 

    public GpsPlaceSearch(Activity context){ 
     this.context=context; 
     googleApiClient=getInstance(); 
     if (googleApiClient!=null){ 
      settingsrequest(); 

      googleApiClient.connect(); 
     } 
    } 

    public LocationRequest locationRequests(){ 
     LocationRequest locationRequest=LocationRequest.create(); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     locationRequest.setInterval(30 * 1000); 
     locationRequest.setFastestInterval(5 * 1000); 
     return locationRequest; 
    } 

    public GoogleApiClient getInstance(){ 
     GoogleApiClient mGoogleApiClient=new GoogleApiClient.Builder(context).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 
     return mGoogleApiClient; 
    } 

    public void settingsrequest(){ 
     Log.e("settingsrequest","Comes"); 

     locationRequest1=locationRequests(); 
     LocationSettingsRequest.Builder builder=new LocationSettingsRequest.Builder().addLocationRequest(locationRequest1); 

     builder.setAlwaysShow(true); 
     final PendingResult<LocationSettingsResult> result= LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
     result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
      @Override 
      public void onResult(LocationSettingsResult result) { 
       Status status=result.getStatus(); 
       LocationSettingsStates states=result.getLocationSettingsStates(); 
       switch (status.getStatusCode()){ 
        case LocationSettingsStatusCodes.SUCCESS: 

         break; 
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
         try{ 
          status.startResolutionForResult(context,REQUEST_CHECK_SETTINGS); 
         } catch (IntentSender.SendIntentException e) { 
          e.printStackTrace(); 
         } 
         break; 
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 

         break; 
       } 
      } 
     }); 
    } 

    @RequiresApi(api = Build.VERSION_CODES.M) 
    @Override 
    public void onConnected(Bundle bundle) { 
     location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient); 
     if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ 
      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108); 
      LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this); 

     } 
     else { 

      currentLatitude = location.getLatitude(); 
      currentLongitude = location.getLongitude(); 

      Toast.makeText(this, currentLatitude + " AND " + currentLongitude + "", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

     if (connectionResult.hasResolution()){ 
      try { 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } 
     else { 
      Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Activity mActivity=this; 
     GpsPlaceSearch gpsPlaceSearch=new GpsPlaceSearch(mActivity); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     //Now lets connect to the API 
     getGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     if (getGoogleApiClient.isConnected()){ 
      LocationServices.FusedLocationApi.removeLocationUpdates(getGoogleApiClient,this); 
      getGoogleApiClient.disconnect(); 
     } 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     LocationSettingsStates states=LocationSettingsStates.fromIntent(data); 
     switch (requestCode){ 
      case REQUEST_CHECK_SETTINGS: 
       switch (resultCode){ 
        case Activity.RESULT_OK: 
         break; 
        case Activity.RESULT_CANCELED: 
         startActivity(new Intent(getApplicationContext(),Login_page.class)); 
         break; 
       } 
       break; 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     currentLatitude = location.getLatitude(); 
     currentLongitude = location.getLongitude(); 
     Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_FINE_LOCATION: 
       if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){ 

       } 
       else { 

       } 
       return; 
     } 
    } 
} 

Logcat:

FATAL AUSNAHME: Haupt Prozess: com.bigfoot.trialapp, PID: 17660 java.lang.IllegalArgumentException: GoogleApiClient-Parameter ist erforderlich. bei com.google.android.gms.common.internal.zzaa.zzb (Unknown Source) bei com.google.android.gms.location.LocationServices.zzj (Unbekannt Quelle) bei com.google.android. gms.location.internal.zzd.getLastLocation (Unbekannt Quelle) bei com.bigfoot.trialapp.GpsPlaceSearch.onConnected (GpsPlaceSearch.java:128) bei com.google.android.gms.common.internal.zzk.zzp (Unbekannte Quelle) bei com.google.android.gms.internal.zzrd.zzn (Unbekannte Quelle) bei com.google.android.gms.internal.zzrb.zzass (Unbekannte Quelle) unter com.google.android.gms.internal.zzrb.onConnected (Unbekannte Quelle) unter com.google.android.gms.internal.zzrf.onConnected (Unbekannte Quelle) unter com.google.android.gms.internal.zzqr .onConnected (Unbekannte Quelle) bei com.google.android.gms.common.internal.zzj $ 1.onConnected (Unbekannt Quelle) bei com.google.android.gms.common.internal.zze $ zzj.zzavj (Unbekannt Quelle) bei com.google.android.gms.common.internal.zze $ zza.zzc (Unbekannte Quelle) bei com.google.android.gms.common.internal.zze $ zza.zzv (Unbekannte Quelle) unter com.google.android.gms.common.internal.zze $ zze.zzavl (Unbekannt Source) bei com.google.android.gms.common.internal.zze $ zzd.handleMessage (Unbekannt Quelle) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os. Looper.loop (Looper.java:148) bei android.app.ActivityThread.main (ActivityThread.java:5443) bei java.lang.reflect.Method.invoke (native Methode) um com.android.internal. os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:728) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 618)

+2

Also, was mit Ihrem aktuellen geschieht entfernen sollten Code? Können Sie es auf ein eher minimales Beispiel reduzieren? (Sie haben im Moment fast 200 Zeilen Code geschrieben - ich bezweifle, dass es * komplett * erforderlich ist, um das Problem zu zeigen.) –

+0

Was ist bei Zeile Nr. 128 in Ihrer Klasse? –

+0

es gibt mir null Zeiger Ausnahme, dass googleapiclient Parameter erforderlich ist, aber ich habe die googleapiclient –

Antwort

0

Sie unter der Linie von onConnected nur

location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient); 

und sollte requestLocationUpdates Methode aufrufen in onConnected

@RequiresApi(api = Build.VERSION_CODES.M) 
@Override 
public void onConnected(Bundle bundle) { 

    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ 
     requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108); 
     LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this); 

    } 
}else{ 
    //custom dialog for "please enable location permission to access device location" 
} 
+0

Warum nicht 'getLastLocation' von' onConnected' aufrufen? – Atul