Google Play In-App Reviews API java.lang.ClassCastException

253 views Asked by At

After implementing google play In-App reviews API in my app i keep getting ClassCastException crash in firebase.

Fatal Exception: java.lang.ClassCastException: android.os.RemoteException cannot be cast to y4.a
   at .MainActivity.lambda$review_dialog$3(MainActivity.java:31)
   at com.google.android.gms.tasks.zzi.run(zzi.java:21)
   at android.os.Handler.handleCallback(Handler.java:938)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loopOnce(Looper.java:201)
   at android.os.Looper.loop(Looper.java:288)
   at android.app.ActivityThread.main(ActivityThread.java:7886)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:568)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
public void review_dialog(){
        Task<ReviewInfo> request = manager.requestReviewFlow();
        request.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                // We can get the ReviewInfo object
                ReviewInfo reviewInfo = task.getResult();
                Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                flow.addOnCompleteListener(task1 -> {
                    // The flow has finished. The API does not indicate whether the user
                    // reviewed or not, or even whether the review dialog was shown. Thus, no
                    // matter the result, we continue our app flow.
                });
            } else {
                // There was some problem, log or handle the error code.
                @ReviewErrorCode int reviewErrorCode = ((ReviewException)    task.getException()).getErrorCode();
            }
        });



    }

is the problem in (ReviewException) casting?

2

There are 2 answers

0
S-Sh On BEST ANSWER

It's a bad practice to perform downcasting without either checking type first or handling the cast error properly. The exception in the case of failure in the completion Listener of the task can be caused by different reasons, not necessary some ReviewException problem. So, check it first:

 if (task.isSuccessful()) {
                // We can get the ReviewInfo object
                ReviewInfo reviewInfo = task.getResult();
                Task<Void> flow = manager.launchReviewFlow(this, reviewInfo);
                flow.addOnCompleteListener(task1 -> {
                    // The flow has finished. The API does not indicate whether the user
                    // reviewed or not, or even whether the review dialog was shown. Thus, no
                    // matter the result, we continue our app flow.
                });
            } else {
                // There was some problem, log or handle the error code.
                Exception exception = task.getException();
                if (exception instanceof ReviewException) {
                    // It's safe to downcast now
                    @ReviewErrorCode int reviewErrorCode = ((ReviewException)    task.getException()).getErrorCode();
                } else {
                     // some other error
                }
            }
1
Mst. Anjuara Begum On
import com.google.android.play.core.tasks.OnCompleteListener;
import com.google.android.play.core.tasks.Task;
import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.review.ReviewRequest;
import com.google.android.play.core.tasks.Task;
import com.google.android.play.core.tasks.TaskCompletionSource;

public class InAppReviewExample {

    public void requestInAppReview() {
        ReviewManager reviewManager = ReviewManagerFactory.create(context);

        // Create a ReviewRequest instance.
        ReviewRequest reviewRequest = reviewManager.requestReviewFlow();

        // Use a TaskCompletionSource to handle the result.
        TaskCompletionSource<ReviewInfo> taskCompletionSource = new TaskCompletionSource<>();

        // Handle the Task using an OnCompleteListener.
        reviewRequest.addOnCompleteListener(new OnCompleteListener<ReviewInfo>() {
            @Override
            public void onComplete(Task<ReviewInfo> task) {
                try {
                    if (task.isSuccessful()) {
                        // Get the ReviewInfo object.
                        ReviewInfo reviewInfo = task.getResult();
                        
                        // Use the ReviewInfo object to launch the in-app review flow.
                        Task<Void> flowTask = reviewManager.launchReviewFlow(context, reviewInfo);
                        flowTask.addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(Task<Void> task) {
                                if (task.isSuccessful()) {
                                    // In-app review flow launched successfully.
                                } else {
                                    // In-app review flow failed to launch.
                                }
                            }
                        });
                    } else {
                        // There was an error getting the ReviewInfo.
                        Exception exception = task.getException();
                        if (exception instanceof ClassCastException) {
                            // Handle the ClassCastException here.
                            // Example: Log an error message or perform necessary actions.
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        // You can also handle the task using await() or continueWith() if needed.
        // taskCompletionSource.setResult(reviewInfo); // Set the result when available.
    }
}