我試圖運行一項服務,該服務使用Google Play位置服務跟蹤用戶的位置。我遵循[Android Developers Google Play](http://developer.android.com/google/play-services/setup.html)指示,但目前有一個崩潰的應用程序。Google Play服務位置丟失NullPointerException
我正在使用帶有Google API目標(17)的android模擬器AVD。
我
1)安裝了谷歌Play服務SDK
2)取得了谷歌的副本Play服務庫項目,導入到我的Eclipse工作區,並在我目前的應用程序中引用它,使用這些[說明](http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject)
我不知道我是否沒有任何補充的清單
一些背景知識在我的應用程序結構上:我創建了一個包含所有錯誤處理的位置服務類,並通過服務調用該類的方法。我的應用程序崩潰目前,當我打電話:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
命令,它是在servicesConnected()
這是第一個例子中,我叫什麼谷歌播放相關。所以基本上,我在哪裏的:我想調用的方法檢查,如果谷歌播放服務是可用的,和應用程序崩潰的:
08-19 03:51:19.464: E/AndroidRuntime(1112): java.lang.RuntimeException: Unable to start service
[email protected] with Intent { flg=0x4
cmp=com.scarr025.zwilio/.AdventureService (has extras) }: java.lang.NullPointerException
請幫助!
代碼如下:
谷歌播放服務類
public class AdventureLocator extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
protected void onCreate(Bundle savedInstanceState) {
mLocClient = new LocationClient(this, this, this);
mContext = this;
}
public void startConnection() {
mLocClient.connect();
}
public Location getLastLocation() {
return mLocClient.getLastLocation();
}
// Method that encapsulates the check for Google Play services
public boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("$$AdventureLocator$$", "In servicesConnected; Google Play services is available (returned true).");
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Get the error code
// Get the error dialog from Google Play services
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
resultCode,
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
// Set the dialog in the DialogFragment
errorFragment.setDialog(errorDialog);
// Show the error dialog in the DialogFragment
errorFragment.show(getSupportFragmentManager(), "Location Updates");
}
return false;
}
...
清單文件
...
<service
android:name=".AdventureService"
android:label="@string/title_adventure_service" >
</service>
<activity
android:name=".AdventureLocator"
android:label="@string/title_adventure_locator" >
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
考慮添加網絡權限 – Nizam