Android O Preview findViewById compile error

一世执手 提交于 2019-12-08 16:31:55

问题


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.


回答1:


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




回答2:


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...




回答3:


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.




回答4:


I believe they changed the method signature of findViewById so that you don't require the cast anymore. Try change that line of code to

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


来源:https://stackoverflow.com/questions/44126828/android-o-preview-findviewbyid-compile-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!