2017-04-18 69 views
-1

這種隨機發生的,尤其是在Genymotion模擬器 我認爲這個問題是該行:PlaceDetectionApi示出了粘結劑調用失敗(安卓 - NullPointerException異常)

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null); 

下面我讓所有的代碼:

**MainActivity** 

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener { 

    private GoogleApiClient mGoogleApiClient; 
    private static final int GOOGLE_API_CLIENT_ID = 0; 
    private static final int PERMISSION_REQUEST_CODE = 100; 
    int PLACE_PICKER_REQUEST = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button currentButton = (Button) findViewById(R.id.currentButton); 

     mGoogleApiClient = new GoogleApiClient 
       .Builder(this) 
       .addApi(Places.GEO_DATA_API) 
       .addApi(Places.PLACE_DETECTION_API) 
       .enableAutoManage(this, this) 
       .build(); 

     currentButton.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       if(mGoogleApiClient.isConnected()){ 
        if(ContextCompat.checkSelfPermission(MainActivity.this, 
          Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ 
         ActivityCompat.requestPermissions(MainActivity.this, 
           new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE); 
        }else{ 
         callPlaceDetectionApi(); 
        } 
       } 
      } 
     }); 
    } 

    @Override 
    protected void onStart() { 
     mGoogleApiClient.connect(); 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 


    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     Log.e(String.valueOf(this.getClass()), "Google Places API connection failed with error code: " + 
     connectionResult.getErrorCode()); 

     Toast.makeText(this, "Google Places API connection failed with error code: " + connectionResult.getErrorCode() 
     , Toast.LENGTH_LONG).show(); 
    } 

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

    private void callPlaceDetectionApi() throws SecurityException { 
     PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi 
       .getCurrentPlace(mGoogleApiClient, null); 
     result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { 
      @Override 
      public void onResult(@NonNull PlaceLikelihoodBuffer placeLikelihoods) { 
       for(PlaceLikelihood placeLikelihood : placeLikelihoods){ 
        Log.i(String.valueOf(this.getClass()), String.format("Place '%s' with " + 
            "likelihood: %g", 
          placeLikelihood.getPlace().getName(), 
          placeLikelihood.getLikelihood())); 
       } 
       placeLikelihoods.release(); 
      } 
     }); 
    } 
} 

Log 

活頁夾調用失敗

java.lang.NullPointerException:試圖調用virtu人的方法 '無效 com.google.android.gms.location.places.zzo $ zzf.zzb(com.google.android.gms.common.api.Result)' 上的空對象引用

在玉米.google.android.gms.location.places.zzo.zzbs(來源不明)

在 com.google.android.gms.location.places.internal.zzl $ zza.onTransact(未知 來源)

at android.os.Binder.execTransact(Binder.java:565)

+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -它) – BlackHatSamurai

回答

0

最有可能的Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);返回null。您應該設置一個斷點並逐步執行代碼並找出哪些爲空。

相關問題