Android O preview findViewById compilation error

I tried to test the second stage of the Android O Developer preview. After the project was created, I just clicked build and run, but I had no success.

Default generated code for Android by default:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

A compilation error has occurred.

 Error:(18, 37) error: reference to findViewById is ambiguous both method findViewById(int) in Activity and method <T>findViewById(int) in AppCompatActivity match where T is a type-variable: T extends View declared in method <T>findViewById(int) 

Help me! How to fix this error?

Edit # 1

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } 

=> compile Error

Error: (18, 27) error: the reference to findViewById is ambiguous both findViewById (int) methods in Activity and findViewById (int) method according to AppCompatActivity where T is a variable of type: T extends View is declared in findViewById (int) method

This is not a casting issue.

My build.gradle is here.

 apply plugin: 'com.android.application' android { compileSdkVersion 'android-O' buildToolsVersion "26.0.0-rc2" defaultConfig { applicationId "com.example.app" minSdkVersion 16 targetSdkVersion 'O' versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.0-beta1' testCompile 'junit:junit:4.12' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.0.0-beta1' } 

I tried Android Oreo Developer Preview 2. And use Android Studio 3.0 Canary version.

+7
android findviewbyid android-o ambiguous
source share
4 answers

Your build.gradle looks good, but the compiler is still compiling with Lib 26 support against the older compileSdkVersion (25 or lower).

Try syncing gradle and Build-> Clean Project. If this does not help, File-> Invalidate Cache / Restart should do something ...

+1
source share

The signature of the findViewById method was changed with the introduction of API level 25 to support generics and remove ugly castings:

New method signature:

 public <T extends View> T findViewById (int id); 

compared to the old:

 public View findViewById(int id); 

So change your code to:

 Toolbar toolbar = findViewById(R.id.toolbar); 

Link: View | Android Developer

0
source share

I faced the same problem when my compileSdkVersion team was 27 and buildToolsVersion was not 27. CompileSdkVersion 27 changed them buildToolsVersion "27.0.0" I think this happens when buildToolsVersion is older than compileSdkVersion.

0
source share

I believe that they changed the signature of the findViewById method so that they no longer require a cast. Try changing this line of code to

 Toolbar toolbar = findViewById(R.id.toolbar); 
-one
source share

All Articles