Android O预览findViewById编译错误 [英] Android O Preview findViewById compile error

查看:112
本文介绍了Android O预览findViewById编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试Android O Developer Preview第二阶段. 创建项目后,我只是单击构建并运行",但没有成功.

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默认生成的代码:

Android default generated code below:

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

发生编译错误.

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?

编辑#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();
        }
    });
}

=>编译错误

错误:(18,27)错误:对findViewById的引用不明确 Activity中的方法findViewById(int)和AppCompatActivity中的方法findViewById(int)都匹配 其中T是类型变量: T扩展了在方法findViewById(int)中声明的View

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)

这不是强制转换问题.

我的build.gradle在这里.

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'
}

我尝试使用Android Oreo Developer Preview 2. 并使用Android Studio 3.0 Canary版.

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

推荐答案

您的build.gradle看起来不错,但是编译器似乎仍然可以使用supportLib 26针对较早的 compileSdkVersion (25或更低版本)进行编译.

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

尝试同步gradle和 Build-> Clean Project . 如果这样做没有帮助,则 File-> Invalidate Cache/Restart 应该会执行此操作...

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

这篇关于Android O预览findViewById编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆