0

我正在使用Google Play LocationClient。Android GooglePlay LocationClient爲null,儘管已在onCreate()中初始化

mLocationClient = new LocationClient(this, this, this); 

,我在做在onStart()

mLocationClient.connect(); 

它工作在我的Android手機很好,但在開發商連:我在作爲文檔中闡明的onCreate()初始化它控制檯我發現在connect()行中發生了NullPointerException。 這是怎麼發生的?

+0

你能發表更多的代碼嗎? – jimmyC

+0

onCreate()中發生了很多事情。初始化TextViews,Variables,ActionBar,Fragments,...不知道這將如何幫助。它適用於我的設備和另一個 – MrHill

+1

將LogCat輸出發佈到您看到的異常 –

回答

0

這可能是因爲導致NPE的設備沒有Google Play服務或未達到日期。在onCreate你可以檢查

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
if (errorCode != ConnectionResult.SUCCESS) 
{ 
     if (DEBUG) {Log.d(TAG, "errorCode = " + errorCode);} 
     Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, this, 
           1, new DialogCancelListener()); 
     errorDialog.show(); 
} 

這將顯示一個對話框,要求用戶去谷歌商店更新或安裝。
在onStart你需要檢查爲空

if (mLocationClient != null) 
{ 
    mLocationClient.connect(); 
} 
+0

謝謝。那是真的。當GooglePlayServices不可用並且NullPointer發生時,會調用onStart() – MrHill

相關問題