模糊方法调用getClass()

[英]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上的错误(红线):

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?

有人能给我解释一下是怎么回事吗?为什么代码还在运行?

7 个解决方案

#1


254  

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

谷歌与JetBrains公司合作开发了这款应用。同样的bug也会发生在IntelliJ中。看一下错误报告好吗

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

解决这个问题的唯一方法是将调用getClass()的实例转换为对象,如下所示:

((Object) this).getClass()

#2


73  

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。

#3


9  

Cast your "getClass" to an Object, use

将“getClass”转换为对象,使用

((Object) this).getClass()

For those having this problem with fragments, use

对于那些有碎片问题的人,请使用。

((Object) fragment).getClass()

#4


8  

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.

相关的IntelliJ问题也在这里。

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...

当这个bug被修复时,您可以将文件重命名为原来的名称。

#5


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.

今天我在创建一个新项目时遇到了同样的问题。我比较了另一个没有这个问题的项目,发现了一个不同。旧项目是基于“Android 4.2.2”构建的,而新项目默认设置为“Android API 19平台”。我把它改成了“Android 4.2.2”,它等于API 17,红色的错误标记消失了。API 17对于我的项目来说已经足够了,所以我可以这样做。老实说,我不知道为什么这能解决问题。

#6


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.

我找到了解决办法,至少在我这方面。这肯定是一个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.

我在Jetbrains问题跟踪器上发布了相同的信息,所以希望我们很快就能看到解决方案。

#7


1  

Just use fragment.class.getSimpleName();

只使用fragment.class.getSimpleName();


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/29/af6b426f3b79702665cff568f606daf0.html



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