dimanche 29 mars 2015

Casting to an indirect subclass



I have been playing around with the google drive api and am having a hard time understanding why I cannot cast Result to DriveFolder.DriveFolderResult. In the documentation DriveFolder.DriveFolderResult is a indirect known subclass of Result, but when I try casting Result to DriveFolder.DriveFolderResult I get the exception,



`java.lang.ClassCastException: com.google.android.gms.drive.internal.v$e cannot be cast to com.google.android.gms.drive.DriveApi$DriveIdResult`


Why is this happening? Since DriveFolder.DriveFolderResult is a indirect known subclass shouldn't I be able to cast it to 'Result'?


Also to help you guys I pasted some code down below. This is taken directly from here. Why would this code work? How can I implement the interface ResultCallback to give me back DriveFolder.DriveFolderResult? The best solution for me would be to have a class implement ResultCallback, have the method onResult return some object that has the method getStatus() and I then can cast it to any indirect subclass I want. Is this possible?



ResultCallback<DriveFolderResult> folderCreatedCallback = new
ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
showMessage("Created a folder: " + result.getDriveFolder().getDriveId());
}
}



Aucun commentaire:

Enregistrer un commentaire