Google Play服务位置抛出NullPointerException

时间:2013-08-19 04:31:34

标签: android nullpointerexception location google-play-services

我正在尝试使用Google Play定位服务运行跟踪用户位置的服务。我已经按照[Android开发者Google Play](http://developer.android.com/google/play-services/setup.html)说明进行操作,但目前有一个崩溃的应用程序。

我正在使用Android模拟器AVD和Google API目标(17)。

我有

1)安装了Google Play服务SDK

2)制作了Google Play服务库项目的副本,将其导入我的Eclipse工作区,并使用这些[说明](http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject

在我当前的应用中引用它

我不确定是否必须向Manifest添加任何内容

我的app结构的一些背景:我创建了一个包含所有错误处理的Location Services类,并通过服务调用该类的方法。当我打电话给:

时,我的应用程序正在崩溃

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"/>

1 个答案:

答案 0 :(得分:0)

遗漏了在清单文件中引用的库

“在您添加Google Play服务库作为应用项目的依赖项后,打开应用的清单文件,并将以下标记添加为元素的子代:

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

在设置项目以引用库项目后,您可以开始使用Google Play服务API开发功能。“