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

旧城冷巷雨未停 提交于 2019-12-30 18:46:37

问题


I'm attempting to follow Google's Activity Recognition example.

In the example they start the activity recognition when you click a button, however I'd like to just start it up when the app starts - so I tried putting it in the onConnected method. However, it ends up throwing a null pointer exception.

Here's how I'm calling it:

@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);
    }
}

And the error:

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$2.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

Any ideas what I need to do differently to avoid this error? Other location services work just fine - like geofencing or requesting activity updates.


回答1:


The problem ended up that I was adding the LocationServices.API to the Google API Client, but I wasn't adding ActivityRecognition.API. After I updated my call to this, it worked:

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


来源:https://stackoverflow.com/questions/29190072/android-activityrecognition-java-lang-nullpointerexception-appropriate-api-was

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!