Error : Ambiguous method call. Both findViewById (

2019-03-22 14:46发布

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.enter image description here

13条回答
狗以群分
2楼-- · 2019-03-22 15:09

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.

查看更多
聊天终结者
3楼-- · 2019-03-22 15:10

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

查看更多
Anthone
4楼-- · 2019-03-22 15:12

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楼-- · 2019-03-22 15:13

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.

查看更多
祖国的老花朵
6楼-- · 2019-03-22 15:14

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.

查看更多
贼婆χ
7楼-- · 2019-03-22 15:18

Maybe you have difference between compileSdkVersion and targetSdkVersion

查看更多
登录 后发表回答