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

23

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

Multicolored answered 24/10, 2017 at 0:7 Comment(0)
U
50

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.

Unearth answered 2/7, 2018 at 5:16 Comment(0)
Y
11

Upgrade to appcompat 27 solved this for me

Yong answered 31/10, 2017 at 7:35 Comment(2)
That's not recommended, as lint will tell you: This support library should not use a different version (27) than the compileSdkVersion (26)Hirsute
@Hirsute To follow this solution, you also need to update compleSdkVersion to 27.Recapitulate
R
7

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

Roseola answered 5/2, 2019 at 13:36 Comment(1)
for me also this is the issue. thank you.Sasaki
D
4

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.

Dissent answered 24/10, 2017 at 1:30 Comment(3)
This was a good catch but doesn't work for me. I have also tried Clean Project and Invalidate cache.Multicolored
It should word definitely. Guess you have one (or few) more imports like import __something__.ActivityDissent
As long as I remove import android.app.Activity it gets added again automatically. Don't know what to do.Diligent
M
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'
Monosome answered 28/1, 2018 at 5:40 Comment(0)
V
4

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.

Vein answered 30/1, 2018 at 18:42 Comment(1)
Same issue for me. And changing compileSdk to 27 don't resolved. But, changing com.android.support to 27.1.0 (design and support-v4) resolved the issue.Tanta
Q
3

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

Quay answered 1/11, 2017 at 7:17 Comment(1)
For me this was required in addition to fixing the library and SDK versions.Hirsute
F
2

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.

Firstfoot answered 28/5, 2018 at 7:39 Comment(0)
L
2

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.

Laszlo answered 27/8, 2018 at 20:18 Comment(1)
thats the solution, I just temporarily change and then revert back the build-tools version and it worked. But I wonder whats the real problem?.Featheredge
B
1

Maybe you have difference between compileSdkVersion and targetSdkVersion

Boresome answered 28/10, 2017 at 12:5 Comment(0)
H
1

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.

Hirsute answered 12/11, 2017 at 19:34 Comment(0)
I
1

In my case, I removed the constraint layout dependency in the build.gradle (app) file and it solved the issue.

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:25.1.0'
  //implementation 'com.android.support.constraint:constraint-layout:2.0.4'
}
Inbound answered 23/5, 2021 at 13:17 Comment(0)
P
0

if answers does not resolve your problem , you can reset your android studio. I solved with reset my android studio. For reset: https://mcmap.net/q/35966/-how-to-reset-android-studio

Perfect answered 19/1, 2018 at 14:24 Comment(0)
M
0

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.

Midkiff answered 31/5, 2018 at 7:50 Comment(0)
D
0

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.

Degrade answered 3/1, 2020 at 5:39 Comment(2)
Your answer should be a comment.It adds little to the already existing answersPolypropylene
Noted. Won't repeat this.Degrade
B
0

Removing "Android Fast Networking" library solved my problem

Bield answered 6/3, 2020 at 18:53 Comment(0)
C
0

I've had the same issue
Mine was related to the compileSdkVersion number from build.gradle app.
Changed to the latest and it worked for me.

Canning answered 17/4, 2020 at 20:2 Comment(0)
S
0

Upgrading your targetSdkVersion or buildToolsVersion might resolve the problem.

Subrogate answered 27/8, 2020 at 9:23 Comment(0)
P
0

No need to Invalidate Caches / Restart if this is a new project.

I was working with a new project and in gradle files did not have version numbers set in them, there was "N" across minsdkversion and targetsdkversion, changing to desired sdk version solved the problem

Parthena answered 18/2, 2021 at 7:44 Comment(0)
O
0

Upgrading both appcompat to 27 and compleSdkVersion to 30 works for me.Just upgrade those in gradle build.

Officialism answered 10/4, 2021 at 7:47 Comment(0)
S
0

Just upgrade your minSdkVersion 26 Its works for me.

Symposium answered 14/6, 2021 at 23:10 Comment(0)
D
0

When we create a Android project with minimum SDK lower than 16 and after that we want to add new activity using 'New/Activity/Gallery' we see the indication 'Selected activity templates has minimum SDK level of 16'. So we need to modify 'minSdkVersion' in build.gradle file which leads us to 'Sync Now' process.

If we must build and maintain an app with min SDK lower then 16, we have to change 'constraint layout' to another layout. However when we do this the constraint layout dependency still remains in the dependencies section in the 'build.gradle' file. This caused me the error.

In summary, this is applicable when we need to build a android project with minimum SDK version lower than 16.

  1. When we add a new activity, we change the minSdkVersion in the build.gradle above '16' then click of 'Sync Now'. After that we can choose one of activity template and create a new activity.

  2. Now we change 'constraint layout' in the xml file corresponds to the new activity we created to for example 'linear layout' or 'relative layout'

  3. At this point we make sure delete the dependency which refers to constraint layout in the 'build.gradle' and then click the 'Sync Now' again.

I think the best practice to avoid this error message is to make Android project with minimum SDK 16 and above.

Duello answered 17/11, 2021 at 2:20 Comment(0)
G
0

These steps worked for me on Android Studio for Mac (Arctic Fox v11):

  1. In 'build.gradle' file (for module), change appcompat implementation version to something invalid (e.g. Change 'com.android.support:appcompat-v7:25.1.0' to 'com.android.support:appcompat-v7:25')

  2. Click 'Gradle sync now' and let it finish. This should generate lots of errors

  3. Correct the verison back to original (e.g. 'com.android.support:appcompat-v7:25.1.0'). Click 'Gradle sync now' and let it finish

Original issue with findViewId should disappear

Gyroplane answered 22/11, 2021 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.