There is a bug in IntelliJ that will show an ambiguous method call error when using getClass()
without an explicit cast. It looks like this:
Ambiguous method call. BothgetClass () in Object andgetClass () in Object match.
This is a false positive, as you can still compile and run your application fine. There are two accepted workarounds to remove Android Studio from reporting this as an error. If you cast all getClass
calls with Object
, it will remove the error. There is a better workaround that will remove the erroneous error that does not require you to modify your code.
Steps
- Open Finder
- Navigate to the target SDK’s source. For example, if you are targeting API 19, navigate to
$ANDROID_HOME/sources/android-19/java/lang/
- Open
Object.java
in a text editor (such as Atom or TextWranger) - Scroll to
public final native Class<?> getClass();
(line 241 for API 19) - Remove the unbounded wildcard so the line now looks like:
public final native Class getClass();
- Save the file, and your erroneous errors should disappear