gpt4 book ai didi

Android Activity 识别 java.lang.NullPointerException : Appropriate Api was not requested

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:44 27 4
gpt4 key购买 nike

我正在尝试关注 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$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

有什么想法可以避免此错误吗?其他位置服务工作得很好——例如地理围栏或请求 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();
}

关于Android Activity 识别 java.lang.NullPointerException : Appropriate Api was not requested,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com