Android O Preview findViewById compile error

Viewed 7235

I tried to test the Android O Developer Preview second phase. After the project was created, I just clicked build and run but I didn't have any success.

Android default generated code below:

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

compile error 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 do I 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: reference to findViewById is ambiguous both method findViewById(int) in Activity and method findViewById(int) in AppCompatActivity match where T is a type-variable: T extends View declared in method findViewById(int)

This is not 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 to Android Oreo Developer Preview 2. And use Android Studio 3.0 Canary edition.

4 Answers

I encountered the same problem when my compileSdkVersion was 27 and buildToolsVersion was not 27. Changed them to compileSdkVersion 27 buildToolsVersion "27.0.0" I think it happens when the buildToolsVersion is older than the compileSdkVersion.

Related