In the code snippet below, why would lines 1 & 2 be fine and line 3 cause a compile error? Aren't the first two lines functionally equivalent to the third?
Loader loader = getLoaderManager().getLoader(0);
PatientLoader patientLoader = (PatientLoader) loader;
patientLoader = (PatientLoader) getLoaderManager().getLoader(0); // ERROR!
Throws this:
java: somepath/Patient.java:99: inconvertible types
found : android.content.Loader<java.lang.Object>
required: com.example.package.PatientLoader
PatientLoader
indirectly extends Loader<>
.
I come from a C# background and in C# this would not be a problem, so maybe I'm missing something about the Java type system.
PatientLoader
extends AsyncTaskLoader<Cursor>
. And anyone familiar w/ Android SDK would know AsyncTaskLoader<>
extends Loader<>
.
It has nothing to do with the placement of the parenthesis. This problem has to do with Generics:
E.g. Compilation failure:
Loader<Object> loader = getLoaderManager().getLoader(0);
PatientLoader ch = (PatientLoader)loader; // Will show compile error (Cannot cast from Loader<Object> to PatienLoader)
But this will compile fine:
Loader<?> loader = getLoaderManager().getLoader(0);
PatientLoader ch = (PatientLoader)loader; // Compiles fine.
The difference is the <Object> generic versus the <?> generic declaration.
The problem is that getLoader(int) is defined to return a Loader<D>. This means that 'getLoaderManager().getLoader()' in the statement below gets interpreted as a Loader<Object> and not a Loader<?>.
PatientLoader ch = (PatientLoader)getLoaderManager().getLoader(0); // Compile error.
I think this is a 'bug' in the SDK. The method getLoader(int) should have been defined to return a Loader<?> and not a Loader<D>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With