Android Studio 3 中 Executor 的编译错误
Compilation error of Executor in Android Studio 3
我的代码在下面并遵循本文以在 Android Studio 3 中实现 Recaptcha:https://developer.android.com/training/safetynet/recaptcha.html
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(this).verifyWithRecaptcha("api key")
.addOnSuccessListener((Executor) this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
}
}
})
.addOnFailureListener((Executor) this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
我遇到以下编译错误。
可转换类型:无法将匿名 android.view.view.onclicklistener 转换为 java.util.concurrent.executor
我错过了什么吗?
问题是this
。在这种情况下 this
是 class View.OnClickListener
,它不是 Activity
.
你应该像这样修复 SafetyNet.getClient(YourActivity.this)
和 (Executor) YourActivity.this
我使用了下面的代码,现在一切正常。
确保在 activity
中实现 Executor
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
.addOnSuccessListener((Activity) MyActivity.this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
我的代码在下面并遵循本文以在 Android Studio 3 中实现 Recaptcha:https://developer.android.com/training/safetynet/recaptcha.html
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(this).verifyWithRecaptcha("api key")
.addOnSuccessListener((Executor) this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
}
}
})
.addOnFailureListener((Executor) this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
我遇到以下编译错误。
可转换类型:无法将匿名 android.view.view.onclicklistener 转换为 java.util.concurrent.executor
我错过了什么吗?
问题是this
。在这种情况下 this
是 class View.OnClickListener
,它不是 Activity
.
你应该像这样修复 SafetyNet.getClient(YourActivity.this)
和 (Executor) YourActivity.this
我使用了下面的代码,现在一切正常。
确保在 activity
中实现 Executorbtn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
.addOnSuccessListener((Activity) MyActivity.this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});