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

后端 未结 17 2197
我寻月下人不归
我寻月下人不归 2021-02-05 00:29

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 appli

相关标签:
17条回答
  • 2021-02-05 00:56

    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.

    0 讨论(0)
  • 2021-02-05 00:57

    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.

    0 讨论(0)
  • 2021-02-05 00:57

    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.

    0 讨论(0)
  • 2021-02-05 01:00

    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.

    0 讨论(0)
  • 2021-02-05 01:00

    Upgrade to appcompat 27 solved this for me

    0 讨论(0)
  • 2021-02-05 01:00

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

    0 讨论(0)
提交回复
热议问题