[英]Android Studio - Ambiguous method call getClass()

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:

我正在為我的Android應用程序使用Android Studio。我的代碼可以工作和編譯。最近,IDE顯示了以下代碼的getClass上的錯誤(紅線):


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?


7 个解决方案



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.

我認為這是Android Studio中的一個bug。我們知道,Android Studio基於IntelliJ平台和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()



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

您可以通過修補Android SDK的源代碼來避免這個bug,而不是修改您的應用程序代碼。

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:

當你遇到getClass()錯誤,去宣言(⌘B在Mac)的方法。這將導航到一個路徑,例如$ANDROID_HOME/sources/android-20/java/lang/Object.java。現在,在IntelliJ或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.
  • 使對象。java可寫通過選擇File ->使文件可寫。如果您試圖編輯文件,可能會自動提示您這樣做。
  • Remove the unbounded wildcard:


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

    //刪除不受限制的通配符(類),以避免http://youtrack.jetbrains.com/addree/idea -72835公共最終類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.

新版本的Android Studio出現了一個bug,它會阻止你編輯文件,即使你聲明它是可寫的。相反,復制路徑,編輯- >復制路徑或⇧⌘C,和你最喜歡的編輯器中編輯它。

This change will preserve source navigation functionality. Other options:


  • You may comment out the entire getClass() declaration.
  • 您可以注釋掉整個getClass()聲明。
  • You may append a non-Java extension to the name of the Object.java file, e.g. Object.java.in.
  • 可以將非java擴展附加到對象的名稱。java文件,例如Object.java.in。



Cast your "getClass" to an Object, use


((Object) this).getClass()

For those having this problem with fragments, use


((Object) fragment).getClass()



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

首先是有關Android Studio的問題。請把它打上星號,以便引起注意!

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.

一個很好的解決方法是重命名 /android- <平台> /java/lang/對象。java Object.java。比如XXX。這將防止人們看到它,從而避免這個問題。當然,通過這樣做,您不再能夠輕松地從AS中導航到對象的源。

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




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.

今天我在創建一個新項目時遇到了同樣的問題。我比較了另一個沒有這個問題的項目,發現了一個不同。舊項目是基於“Android 4.2.2”構建的,而新項目默認設置為“Android API 19平台”。我把它改成了“Android 4.2.2”,它等於API 17,紅色的錯誤標記消失了。API 17對於我的項目來說已經足夠了,所以我可以這樣做。老實說,我不知道為什么這能解決問題。



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.

我找到了解決辦法,至少在我這方面。這肯定是一個IntelliJ bug,但它似乎是由sourcepath中的類和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.

如果您訪問項目結構> SDKs >{{您的Android SDK},請從Sourcepath選項卡刪除任何Android條目。這種變通方法的問題是,您不再能夠直接訪問IntelliJ/Android Studio中的源代碼。

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




Just use fragment.class.getSimpleName();




  © 2014-2022 ITdaan.com 联系我们: