我正在尝试运行一项使用 Google Play 位置服务跟踪用户位置的服务。我已按照 [Android Developers Google Play] ( http://developer.android.com/google/play-services/setup.html ) 说明进行操作,但目前有一个崩溃的应用程序。
我正在使用带有 Google API 目标 (17) 的 android 模拟器 AVD。
我有
1) 安装了 Google Play 服务 SDK
2) 制作 Google Play 服务库项目的副本,将其导入我的 Eclipse 工作区,并在我当前的应用程序中引用它,使用这些 [说明] ( http://developer.android.com/tools/projects/projects- eclipse.html#ReferenceLibraryProject )
我不确定是否必须在清单中添加任何内容
我的应用程序结构的一些背景知识:我创建了一个包含所有错误处理的位置服务类,并通过服务调用该类的方法。当我调用以下命令时,我的应用程序当前正在崩溃:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
这是servicesConnected()
我第一次调用任何与 Google Play 相关的命令。所以基本上,我在哪里:我正在尝试调用方法检查 Google Play 服务是否可用,并且应用程序正在崩溃:
08-19 03:51:19.464: E/AndroidRuntime(1112): java.lang.RuntimeException: Unable to start service
com.scarr025.zwilio.AdventureService@40d24920 with Intent { flg=0x4
cmp=com.scarr025.zwilio/.AdventureService (has extras) }: java.lang.NullPointerException
请帮忙!!
代码如下:
Google Play 服务类
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"/>