130

I'm using Android Studio for my Android application.
My code works and compiles.
Recently, the IDE showes me error (red lines) on getClass of the following code:

fragment.getClass().getSimpleName()

But still the application compiles and runs.
The error is:

Ambiguous method call. Both
getClass () in Object and
getClass () in Object match.

Can some one explain me what is it about? and why the code still running?

NickF
  • 5,637
  • 12
  • 44
  • 75

6 Answers6

255

I think it is a bug in Android Studio. As we know, Android Studio is based on the IntelliJ Platform and the existing functionality of IntelliJ IDEA Community Edition.

Google has developed it in cooperation with JetBrains. And same bug is reported to happen in IntelliJ as well. Have a look at the Error report

The only workaround to this issue is to cast the instance you call getClass() on, to Object as follows:

((Object) this).getClass()
danielcooperxyz
  • 960
  • 1
  • 13
  • 28
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
73

Rather than modify your application code, you can avoid this bug by patching your Android SDK's source code.

When you come across the getClass() error, go to the declaration of the method (⌘B on Mac). This will navigate to a path such as $ANDROID_HOME/sources/android-20/java/lang/Object.java. Now, within IntelliJ or Android Studio:

  • Make Object.java writable by choosing File -> Make File Writable. You may be prompted to do this automatically if you try to edit the file.
  • Remove the unbounded wildcard:

    // Removed unbounded wildcard (Class) to avoid http://youtrack.jetbrains.com/issue/IDEA-72835 public final native Class getClass();

Newer versions of Android Studio appear to suffer from a bug which prevents you from editing the file even after declaring it as writable. Instead, copy the path, Edit -> Copy Path or ⇧⌘C, and edit it in your favorite editor.

This change will preserve source navigation functionality. Other options:

  • You may comment out the entire getClass() declaration.
  • You may append a non-Java extension to the name of the Object.java file, e.g. Object.java.in.
James Wald
  • 13,626
  • 5
  • 52
  • 63
  • James Wald, nice answer,+1 – Ritesh Gune May 14 '14 at 04:47
  • 2
    Actually I think this should be the accepted answer. – Thomas Keller Jul 02 '14 at 08:27
  • 1
    Updated the answer to reflect the fact that Android Studio has a bug which doesn't allow `Object.java` to be edited even after clearing the read-only flag. – James Wald Jul 03 '14 at 02:13
  • 2
    [Here](https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=getClass%20ambiguous%20method%20call&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=70135) is the *Android Studio* bug report. – Phil Jul 16 '14 at 15:02
  • Only commenting the getClass method causes IntelliJ/Studio to complain about `instance.getClass()` (only `getClass()` works). Renaming `Object.java` file to disable it does work. – Pierre-Luc Paour Sep 26 '14 at 12:27
  • Did you also find a similar patch for the Android SDK's source code for other methods from Object? I sometimes get the same ambiguous method call for notify(), wait(long), etc. as well. To those methods, the suggested patch cannot be applied. – Jelle Fresen Oct 23 '14 at 10:10
  • @JelleFresen You'll have to comment out those methods or rename the source file to prevent them from being indexed, or you can add explicit casts to Object as suggested by http://stackoverflow.com/a/18506329/204480. It appears as though this issue has been fixed in EAP IntelliJ builds since August so it should make its way to Android Studio in the future. – James Wald Oct 24 '14 at 07:47
  • Thanks. I seem to have missed the last two suggestions in your post, please excuse me. Good to hear that it may have been fixed. – Jelle Fresen Oct 25 '14 at 10:16
8

First of all the related Android Studio issue is here. Please star it so it can get some attention!

Also the related IntelliJ issue is here.

A good workaround for this is to rename <sdk>/android-<platform>/java/lang/Object.java to Object.java.XXX for instance. This will prevent AS from seeing it and the issue will be avoided. Of course by doing this, you can no longer easily navigate to the source of Object from within AS.

You can rename the file back to its original name when this bug will be fixed...

BoD
  • 10,838
  • 6
  • 63
  • 59
2

Today I ran into the same problem when I created a new project. I compared to another project which did not have this problem and found one difference. The old project was built against "Android 4.2.2" while the new one was by default set to "Android API 19 Platform". I changed that to "Android 4.2.2" which equals API 17 and the red error marker vanished. API 17 is sufficient for my project so I can leave it this way. I have no idea why this resolves the problem, to be honest.

Uwe Post
  • 518
  • 4
  • 10
1

I found a fix for this, at least on my end. It's definitely an IntelliJ bug, but it seems to be caused by a conflict between the classes in the sourcepath and in the classpath for the Android SDK.

If you go to Project Structure > SDKs > {{Your Android SDK}}, remove any Android entries from the Sourcepath tab. The problem with this workaround is that you no longer have direct access to sources from within IntelliJ/Android Studio.

I've posted the same information on the Jetbrains issue tracker, so hopefully we'll see a fix soon.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
1

Just use fragment.class.getSimpleName();

Sotti
  • 14,089
  • 2
  • 50
  • 43