Error : Ambiguous method call. Both findViewById (int) in AppCompactActivity and Activity

回眸只為那壹抹淺笑 提交于 2020-02-18 05:40:13

问题


I am getting the error: "Ambiguous method call" on initializing Toolbar using Android Studio 3.0 RC1. I have extended my Activity with AppCompatActivity and compiling my application using 'compileSdkVersion 26'. Attaching a screenshot of the error.


回答1:


If you recently updated your project to API, Try doing

File -> Invalidate Caches / Restart

then

File -> Sync Project with Gradle Files

this resolve for me.




回答2:


Upgrade to appcompat 27 solved this for me




回答3:


You have

import android.support.v7.app.AppCompatActivity
import android.app.Activity

both in your code. Remove import android.app.Activity as I can see, it's not required for you.




回答4:


For me changing build tool version to 27.0.2 worked, given that all your other dependencies are API 27 as well.

buildToolsVersion '27.0.2'



回答5:


I have recently upgraded to build tools version 27.0.2 and faced the same problem. But noticed that I had the compileSdk version set to 25. Changing compileSdk to 27 resolved the issue.




回答6:


"File - Invalidate Caches / Restart" Solved my problem.




回答7:


For me none of the given solutions worked, however I had this issue because I had both 25 and 27 android sdk installed on my computer. As soon as I removed the sdk 25, the problem disappeared.




回答8:


This can suddenly appear when running a code analyzer like FindBugs-IDEA. A quick way to clear the warning is to temporarily change the buildToolsVersion in your app/build.gradle then change it back again.

For example, follow these steps:

  1. Open your app/build.gradle file.
  2. Change buildToolsVersion to '26.0.1' then sync project (press the 'Sync Now' button when it appears at the top)
  3. Change the buildToolsVersion back to whatever you had before.
  4. Press the 'Sync Now' button

This should clear the error.




回答9:


For me, it was the compileSdkVersion which was different than the support libraries used




回答10:


Maybe you have difference between compileSdkVersion and targetSdkVersion




回答11:


I ran into the same issue with Android Studio 3.0.0 build 171.4408382. Building via Gradle on the command line worked just fine, but the IDE presented me with the above mentioned error. I have tried to use API level 26 with appcompat v26.1.0 and API level 27 with appcompat v27.0.1, but neither combination worked.

My "solution" was to downgrade compileSdkVersion and targetSdkVersion to API level 25 and the appcompat library to version 25.4.0.

For all of the mentioned version combinations I used Gradle plugin v3.0.0 and the Android build tool v27.0.1.




回答12:


if answers does not resolve your problem , you can reset your android studio. I solved with reset my android studio. For reset: https://stackoverflow.com/a/19397632/3129822




回答13:


First make sure that you have no transitive dependencies that are using older support libraries. Run the following command and make sure that no older support libraries are in use.

gradlew :app:dependencies

Make sure that your gradle file is updated with latest dependencies. eg: compileSdkVersion 27, targetSdkVersion 27, buildToolsVersion 27.0.3. etc. It would also benefit to make sure that non of your app flavors have been using a custom targetSdkVersion.

Ctrl + click on the findViewById method. It will show you the 2(perhaps more?) conflicting methods. In my case the conflict was between the findViewById method from API 23 and API 27. Therefore I had to remove the SDK and sources for Android version 23. Once I removed it and did Invalidate Caches/Restart it solved my problem.




回答14:


My compileSDK version was lower than the targetSDK version so all I had to do was match them and sync project and the error was resolved.



来源:https://stackoverflow.com/questions/46900261/error-ambiguous-method-call-both-findviewbyid-int-in-appcompactactivity-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!