Android ActivityRecognition java.lang.NullPointerException: 没有请求适当的 Api

Android ActivityRecognition java.lang.NullPointerException: Appropriate Api was not requested

我正在尝试遵循 Google 的 Activity Recognition 示例。

在示例中,当您单击按钮时,它们会启动 activity 识别,但是我想在应用程序启动时启动它 - 所以我尝试将其放在 onConnected方法。但是,它最终会抛出一个空指针异常。

我是这样称呼它的:

@Override
public void onConnected(Bundle connectionHint) {
    Log.d(TAG, "onConnected");
    try {
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                mGoogleApiClient,
                UPDATE_INTERVAL_IN_MILLISECONDS,
                getActivityDetectionPendingIntent()
        ).setResultCallback(this);

    } catch (SecurityException securityException) {
        logSecurityException(securityException);
    }
}

错误:

03-21 20:28:57.939    2340-2340/getrewards.example.com.getrewards E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: getrewards.example.com.getrewards, PID: 2340
    java.lang.NullPointerException: Appropriate Api was not requested.
            at com.google.android.gms.internal.jx.b(Unknown Source)
            at com.google.android.gms.common.api.c.a(Unknown Source)
            at com.google.android.gms.common.api.c.a(Unknown Source)
            at com.google.android.gms.common.api.c.b(Unknown Source)
            at com.google.android.gms.internal.nb.requestActivityUpdates(Unknown Source)
            at getrewards.example.com.getrewards.RewardsFragment.onConnected(RewardsFragment.java:274)
            at com.google.android.gms.internal.jm.f(Unknown Source)
            at com.google.android.gms.common.api.c.gJ(Unknown Source)
            at com.google.android.gms.common.api.c.d(Unknown Source)
            at com.google.android.gms.common.api.c.onConnected(Unknown Source)
            at com.google.android.gms.internal.jm.f(Unknown Source)
            at com.google.android.gms.internal.jm.dU(Unknown Source)
            at com.google.android.gms.internal.jl$h.b(Unknown Source)
            at com.google.android.gms.internal.jl$h.g(Unknown Source)
            at com.google.android.gms.internal.jl$b.hy(Unknown Source)
            at com.google.android.gms.internal.jl$a.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-21 20:28:58.343    1235-1594/system_process E/ActivityManager﹕ Invalid thumbnail dimensions: 288x288
03-21 20:29:02.543    1235-1267/system_process E/InputDispatcher﹕ channel '39825fb6 getrewards.example.com.getrewards/getrewards.example.com.getrewards.RewardsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-21 20:33:31.053    2374-2374/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:33:31.053    2374-2374/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:33:44.213    2403-2403/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:33:44.213    2403-2403/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:34:56.658    2479-2479/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:34:56.659    2479-2479/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:35:09.756    2501-2501/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:35:09.756    2501-2501/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:35:52.821    2563-2563/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:35:52.821    2563-2563/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:35:53.299    1235-1267/system_process E/InputDispatcher﹕ channel '21589154 getrewards.example.com.getrewards/getrewards.example.com.getrewards.RewardsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-21 20:36:06.753    2586-2586/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:36:06.753    2586-2586/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:36:29.956    2634-2634/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:36:29.956    2634-2634/? E/android.os.Debug﹕ failed to load memtrack module: -2
03-21 20:36:43.621    2657-2657/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
03-21 20:36:43.621    2657-2657/? E/android.os.Debug﹕ failed to load memtrack module: -2

有什么想法可以避免此错误吗?其他位置服务工作得很好——比如地理围栏或请求 activity 更新。

问题最终是我将 LocationServices.API 添加到 Google API 客户端,但我没有添加 ActivityRecognition.API。在我更新对此的调用后,它起作用了:

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .addApi(ActivityRecognition.API)
                .build();
    }