Android Google Play服务位置抛出NullPointerException
我试图运行一个服务,跟踪用户的位置使用谷歌播放位置服务。我遵循了[Android开发者谷歌游戏]()的说明,但目前有一个崩溃的应用程序 我正在使用android仿真器AVD和Google API目标(17) 我有 1) 安装了Google Play Services SDK 2) 复制Google Play services库项目,将其导入我的Eclipse工作区,并在我当前的应用程序中引用它,使用以下[说明]() 我不确定是否需要在清单中添加任何内容 我的应用程序结构的一些背景:我创建了一个包含所有错误处理的Location Services类,并通过服务调用该类的方法。当我拨打以下电话时,我的应用程序当前正在崩溃:Android Google Play服务位置抛出NullPointerException,android,nullpointerexception,location,google-play-services,Android,Nullpointerexception,Location,Google Play Services,我试图运行一个服务,跟踪用户的位置使用谷歌播放位置服务。我遵循了[Android开发者谷歌游戏]()的说明,但目前有一个崩溃的应用程序 我正在使用android仿真器AVD和Google API目标(17) 我有 1) 安装了Google Play Services SDK 2) 复制Google Play services库项目,将其导入我的Eclipse工作区,并在我当前的应用程序中引用它,使用以下[说明]() 我不确定是否需要在清单中添加任何内容 我的应用程序结构的一些背景:我创建了一个包
GooglePlayServicesUtil.isgoogleplayservicesavaailable(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
请帮忙强>
代码如下:
谷歌播放服务类
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"/>
。。。
遗漏了清单文件中的库引用
...
<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"/>
“将Google Play services库添加为应用程序项目的依赖项后,打开应用程序的清单文件,并将以下标记添加为元素的子项:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
一旦您将项目设置为引用库项目,就可以开始使用Google Play services API开发功能。“考虑添加internet权限