Android Studio 3中Executor的编译错误

时间:2018-04-21 00:31:17

标签: android-studio-3.1

我的代码位于以下文章之后,在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 {
                    }
                }
            });

    }
});

我面临下面的编译错误。

in-convertible类型:无法将匿名android.view.view.onclicklistener强制转换为java.util.concurrent.executor

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

问题是this。在这种情况下,this是类View.OnClickListener,它不是Activity

您应该像SafetyNet.getClient(YourActivity.this)(Executor) YourActivity.this

这样修复

答案 1 :(得分:1)

我使用下面的代码,现在一切正常。

确保在活动

中实施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 {
                    }
                }
            });
    }
});