Android 应用程序在尝试访问 Google Fit API 的 Fitness.getHistoryClient 时崩溃

Android App breaks down when trying to access Fitness.getHistoryClient of the Google Fit API

我正在尝试通过 Google Fit API 的 HistoryClient 访问步数数据,但我收到以下错误消息:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fittesting, PID: 26199
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fittesting/com.example.fittesting.MainActivity}: java.lang.NullPointerException: null reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: null reference
        at com.google.android.gms.common.internal.Preconditions.checkNotNull(com.google.android.gms:play-services-basement@@17.1.1:2)
        at com.google.android.gms.fitness.Fitness.getHistoryClient(com.google.android.gms:play-services-fitness@@20.0.0:14)
        at com.example.fittesting.MainActivity.accessGoogleFit(MainActivity.java:99)
        at com.example.fittesting.MainActivity.onCreate(MainActivity.java:70)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6944) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

导致此错误的代码如下:

    public class MainActivity extends AppCompatActivity {
    public static final int GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = 42;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FitnessOptions fitnessOptions = FitnessOptions.builder()
                .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .build();

        GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);

        if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
            Log.d(TAG, "requesting permissions...");
            GoogleSignIn.requestPermissions(
                    this,
                    GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
                    account,
                    fitnessOptions
            );

        } 

        accessGoogleFit();

    }

    private void accessGoogleFit() {
        Log.d(TAG, "Access Google Fit");

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long startTime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .read(DataType.TYPE_STEP_COUNT_CUMULATIVE)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                    @Override
                    public void onSuccess(DataReadResponse dataReadResponse) {
                        Log.d(TAG, "HistoryClient.readData: onSuccess()");
                        DataSet dataSet;

                        List<DataSet> dataSets = dataReadResponse.getDataSets();
                        Log.d(TAG, "# of DataSets: " + dataSets.size());
                        dataSet = dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_CUMULATIVE);
                        Log.d(TAG, "DataSet: " + dataSet);
                        Log.d(TAG, "DataSource: " + dataSet.getDataSource());
                        Log.d(TAG, "DataType: " + dataSet.getDataType());

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(TAG, "HistoryClient.readData: onFailure()", e);
                    }
                });
    }
}

我认为错误可能是由于缺少或错误的 android 权限引起的,但我无法提取错误的来源。

如果有人知道为什么会发生这种情况,我将非常高兴。

此外,如果缺少一些必要的信息,我很乐意另外提供。

提前致谢!

如@Andy Turner 的评论所述,错误是由返回 'null' 的 GoogleSignIn.getLastSignedInAccount(this) 调用引起的。

原因是 google 云控制台的 OAuth 配置中的 SHA1 证书指纹 与 Android 工作室。 这是由于在 Android Studio 的两个不同安装中编译应用程序造成的。

通过使用第二个 Android Studio 安装的证书添加第二个 OAuth 2.0-Client-ID,问题 得到解决。