有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在安卓 emulator中访问google api时发生java运行时错误

我正在尝试访问googleFit API
这似乎很简单。获取google登录权限和所需授权,然后查询步骤计数
我的代码似乎不起作用
当我调试时,fitnessOption声明部分抛出“源代码与字节码不匹配”错误。我清理了我的项目,重建了它,但它不起作用
安卓大师,我哪里做错了

fun getAuthorizationAndReadData() {
        try {
            MainActivity().fitSignIn(FitActionRequestCode.READ_DATA)
        } catch () {
           Log.i("e", "error!!!!")
        }
       }

主要活动

enum class FitActionRequestCode {
    READ_DATA
}

private val fitnessOptions: GoogleSignInOptionsExtension = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ).build()

    fun fitSignIn(requestCode: FitActionRequestCode) {
        if (oAuthPermissionsApproved()) {
            readHistoryData()
        } else {
            requestCode.let {
                GoogleSignIn.requestPermissions(
                        this,
                        requestCode.ordinal,
                        getGoogleAccount(), fitnessOptions)
            }
        }
    }

    private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)


    private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)

    private fun performActionForRequestCode(requestCode: FitActionRequestCode) = when (requestCode) {
        FitActionRequestCode.READ_DATA -> readHistoryData()
    }
    private fun readHistoryData(): Task<DataReadResponse> {
        // Begin by creating the query.
        val readRequest = queryFitnessData()

        // Invoke the History API to fetch the data with the query
        return Fitness.getHistoryClient(this, getGoogleAccount())
                .readData(readRequest)
                .addOnSuccessListener { dataReadResponse ->
                    printData(dataReadResponse)
                    Log.i(ContentValues.TAG, "Data read was successful!") }
                .addOnFailureListener { e ->
                    Log.e(ContentValues.TAG, "There was a problem reading the data.", e)
                }
    }

    private fun queryFitnessData(): DataReadRequest {
        // [START build_read_data_request]
        // Setting a start and end date using a range of 1 week before this moment.
        val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
        val now = Date()
        calendar.time = now
        val endTime = calendar.timeInMillis
        calendar.add(Calendar.WEEK_OF_YEAR, -1)
        val startTime = calendar.timeInMillis

        return DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build()
    }


共 (1) 个答案

  1. # 1 楼答案

    尝试一下以下步骤

    单击构建->;在设置中清除然后禁用即时运行->;构建、执行、部署