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?
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
}
}