-4

我實現谷歌地圖在我的應用程序和應用程序與MapsActivity下面的錯誤墜毀。java.lang.NumberFormatException:無效INT:「android.permission.ACCESS_COARSE_LOCATION」

我得到了錯誤的OnMapReady()方法。在MapReady中,我調用了displaySettingsRequest,它顯示用戶從應用程序中打開位置服務。

地圖活動:

public class MapsActivity extends BaseActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener {  
private void displayLocationSettingsRequest(Context context) { 
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context) 

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
    result.setResultCallback(result1 -> { 
     final Status status = result1.getStatus(); 
     switch (status.getStatusCode()) { 
      case LocationSettingsStatusCodes.SUCCESS: 
       Log.i(TAG, "All location settings are satisfied."); 
       break; 
      case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
       Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings "); 

       try { 
        // Show the dialog by calling startResolutionForResult(), and check the result 
        // in onActivityResult(). 
        status.startResolutionForResult(MapsActivity.this, HawkConstants.REQUEST_CODE); 
       } catch (IntentSender.SendIntentException e) { 
        Log.i(TAG, "PendingIntent unable to execute request."); 
       } 
       break; 
      case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
       Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created."); 
       break; 
     } 
    }); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    displayLocationSettingsRequest(App.getContext()); 
    mMap = googleMap; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {    
     return; 
    } 
    mMap.setMyLocationEnabled(true); 

} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {   
     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 
       Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION)); 
     return; 
    } 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
} 
+0

告訴我們代碼。關於'MapsActivity' – AJay

+0

喜@AJay我已經編輯我的代碼問題,請看看 – Durga

+0

這個問題與你的進口檢查的進口爲'Manifest'應該只有一個是進口線。 – AJay

回答

1

你想:

Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION)); 

Manifest.permission.ACCESS_COARSE_LOCATION不是十進制數字的String表示。它會導致NumberFormatException

你堆棧跟蹤說,該Manifest.permission.ACCESS_COARSE_LOCATION「android.permission.ACCESS_COARSE_LOCATION」。你只能解析String就像"10""1234123""-11121",但是Java將無法解析"java cute string"爲數字。

你可以閱讀更多關於NFEhere

+0

謝謝你的幫助,是的,在StackTrace中的例外說明你已經清楚地解釋了,但根本原因是我的錯誤,我沒有正確處理權限模型。我錯過了處理,在用戶拒絕位置許可後。 – Durga

相關問題