为 Google Fit 创建自定义数据类型
Creating Custom DataType for GoogleFit
我一直在尝试为 google 拟合应用程序创建自定义数据类型,但 运行 出现了一些错误。最初 ConfigApi.createCustomDataType 收到以下错误
"non-static method 'createCustomDataType (com.google.android.gms.common.api.GoolgeApiClient, com.google.android.gms.fitness.request.DataTypeCreateRequest)cannot be referenced from a static context"
所以我实例化了 ConfigApi 以绕过该错误,然后当我 运行 应用程序时出现以下错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用接口方法'void com.google.android.gms.common.api.PendingResult.setResultCallback(com.google.android.gms.common.api.ResultCallback)'
这一行出现错误:pendingResult.setResultCallback
我希望有人能在我开始把事情弄得比现在更糟之前帮助我解决我做错的事情。下面是我正在使用的代码:
// build a request to create a new data type
DataTypeCreateRequest request = new DataTypeCreateRequest.Builder()
.setName(myPackageName)
.addField("custom", Field.FORMAT_INT32)
.build();
// invoke the CONFIG API with (Google API client object and create data type request)
// instantiating ConfigApi due to the following error:
// non-static method createCustomDataType cannot be referenced from a static context
ConfigApi configApi = new ConfigApi() {
@Override
public PendingResult<DataTypeResult> createCustomDataType(GoogleApiClient googleApiClient, DataTypeCreateRequest dataTypeCreateRequest) {
return null;
}
@Override
public PendingResult<DataTypeResult> readDataType(GoogleApiClient googleApiClient, String s) {
return null;
}
@Override
public PendingResult<Status> disableFit(GoogleApiClient googleApiClient) {
return null;
}
};
PendingResult<DataTypeResult> pendingResult =
configApi.createCustomDataType(mClient, request);
/**
* ConfigApi.createCustomDataType was getting the following error:
* non-static method 'createCustomDataType
* (com.google.android.gms.common.api.GoolgeApiClient,
* com.google.android.gms.fitness.request.DataTypeCreateRequest)
* cannot be referenced from a static context
*/
// 3. Check the result asynchronously
// (The result may not be immediately available)
pendingResult.setResultCallback(
new ResultCallback<DataTypeResult>() {
@Override
public void onResult(DataTypeResult dataTypeResult) {
if (dataTypeResult.getStatus().isSuccess()){
DataType customType = dataTypeResult.getDataType();
// Use this custon data type to insert data in your app
onDataTypeAvailable(customType);
}
// Retrieve the created data type
// Use this custom data type to insert data in your app
// (see other examples)
}
}
);
}
您可能想使用 Fitness.ConfigApi 而不是创建您自己的 ConfigApi 实现。
创建 GoogleApiClient 时,确保添加配置 API:
GoogleApiClient client = new GoogleApiClient.Builder()
.addApi(Fitness.CONFIG_API)
...
.build();
扩展 Guga 的第一个答案(有点晚,但以防其他人有同样的问题):
我遇到了完全相同的问题。事实证明,概述中的示例代码(您可能会找到 here, as I did) might not be entirely correct. In an example from Google's ConfigApi documentation (here),一个类似的方法,readDataType()
,被称为不同的方式(尽管错误的调用也在那里重复一次)。
将示例应用到createCustomDataType()
,调用应如下所示:
PendingResult pendingResult = Fitness.ConfigApi.createCustomDataType(mClient, request);
(当然,不要忘记将正确的 API 添加到您的客户端。)
我一直在尝试为 google 拟合应用程序创建自定义数据类型,但 运行 出现了一些错误。最初 ConfigApi.createCustomDataType 收到以下错误
"non-static method 'createCustomDataType (com.google.android.gms.common.api.GoolgeApiClient, com.google.android.gms.fitness.request.DataTypeCreateRequest)cannot be referenced from a static context"
所以我实例化了 ConfigApi 以绕过该错误,然后当我 运行 应用程序时出现以下错误:
java.lang.NullPointerException: 尝试在空对象引用
上调用接口方法'void com.google.android.gms.common.api.PendingResult.setResultCallback(com.google.android.gms.common.api.ResultCallback)'这一行出现错误:pendingResult.setResultCallback
我希望有人能在我开始把事情弄得比现在更糟之前帮助我解决我做错的事情。下面是我正在使用的代码:
// build a request to create a new data type
DataTypeCreateRequest request = new DataTypeCreateRequest.Builder()
.setName(myPackageName)
.addField("custom", Field.FORMAT_INT32)
.build();
// invoke the CONFIG API with (Google API client object and create data type request)
// instantiating ConfigApi due to the following error:
// non-static method createCustomDataType cannot be referenced from a static context
ConfigApi configApi = new ConfigApi() {
@Override
public PendingResult<DataTypeResult> createCustomDataType(GoogleApiClient googleApiClient, DataTypeCreateRequest dataTypeCreateRequest) {
return null;
}
@Override
public PendingResult<DataTypeResult> readDataType(GoogleApiClient googleApiClient, String s) {
return null;
}
@Override
public PendingResult<Status> disableFit(GoogleApiClient googleApiClient) {
return null;
}
};
PendingResult<DataTypeResult> pendingResult =
configApi.createCustomDataType(mClient, request);
/**
* ConfigApi.createCustomDataType was getting the following error:
* non-static method 'createCustomDataType
* (com.google.android.gms.common.api.GoolgeApiClient,
* com.google.android.gms.fitness.request.DataTypeCreateRequest)
* cannot be referenced from a static context
*/
// 3. Check the result asynchronously
// (The result may not be immediately available)
pendingResult.setResultCallback(
new ResultCallback<DataTypeResult>() {
@Override
public void onResult(DataTypeResult dataTypeResult) {
if (dataTypeResult.getStatus().isSuccess()){
DataType customType = dataTypeResult.getDataType();
// Use this custon data type to insert data in your app
onDataTypeAvailable(customType);
}
// Retrieve the created data type
// Use this custom data type to insert data in your app
// (see other examples)
}
}
);
}
您可能想使用 Fitness.ConfigApi 而不是创建您自己的 ConfigApi 实现。
创建 GoogleApiClient 时,确保添加配置 API:
GoogleApiClient client = new GoogleApiClient.Builder()
.addApi(Fitness.CONFIG_API)
...
.build();
扩展 Guga 的第一个答案(有点晚,但以防其他人有同样的问题):
我遇到了完全相同的问题。事实证明,概述中的示例代码(您可能会找到 here, as I did) might not be entirely correct. In an example from Google's ConfigApi documentation (here),一个类似的方法,readDataType()
,被称为不同的方式(尽管错误的调用也在那里重复一次)。
将示例应用到createCustomDataType()
,调用应如下所示:
PendingResult pendingResult = Fitness.ConfigApi.createCustomDataType(mClient, request);
(当然,不要忘记将正确的 API 添加到您的客户端。)