Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O Preview findViewById compile error

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.

like image 689
Kihu Kwon Avatar asked May 23 '17 05:05

Kihu Kwon


2 Answers

Your build.gradle looks good, but the compiler seems still to compile with supportLib 26 against older compileSdkVersion (25 or lower).

Try to sync gradle and Build->Clean Project . If that doesn't help, File->Invalidate Cache / Restart should do the thing...

like image 78
elyte Avatar answered Oct 15 '22 10:10

elyte


The method signature offindViewById was changed with introduction of API-Level 25 to support generics and to remove the ugly casting:

The new method signature:

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

compared to the old one:

public View findViewById(int id);

Therefore change your code to:

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

Reference: View|Android Developer

like image 42
Christopher Avatar answered Oct 15 '22 12:10

Christopher