Android O预览findViewById编译错误

时间:2017-05-23 05:56:39

标签: android ambiguous findviewbyid android-8.0-oreo

我尝试测试Android O Developer Preview的第二阶段。 项目创建完成后,我点击了构建并运行,但我没有取得任何成功。

Android默认生成的代码如下:

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)

救救我!如何解决此错误?

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

=&GT;编译错误

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

中声明的View

这不是投射问题。

我的build.gradle就在这里。

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版本。

4 个答案:

答案 0 :(得分:1)

for(int i=0;i<btn.length;i++){ for(int j=0;j<btn[i].length;j++){ btn[i][j] = new Button(); //Create new button btn[i][j].setMinSize(60,60); //Set size btn[i][j].setText(i+", "+j); //Set coordinates as text btn[i][j].setBackground(background); //Set image GridPane.setColumnIndex(btn[i][j],i); //Set x GridPane.setRowIndex(btn[i][j],j); //Set y root.getChildren().addAll(btn[i][j]); //Add to gridpane btn[i][j].setOnAction(new EventHandler<ActionEvent>() { //Click Handler @Override public void handle(ActionEvent event) { (Button)(event.getSource()).setText("Cliked"); } }); btn[i][j].setOnMouseEntered(new EventHandler<MouseEvent>() { //Mouse enter area Handler @Override public void handle(MouseEvent t) { System.out.println(t.getSource()); } }); } } 的方法签名随着API级别25的引入而改变,以支持泛型并删除丑陋的转换:

新方法签名:

findViewById

与旧版相比:

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

因此请将您的代码更改为:

public View findViewById(int id);

参考:View|Android Developer

答案 1 :(得分:1)

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

尝试同步gradle和 Build-&gt; Clean Project 。 如果这没有帮助,文件 - &gt;无效缓存/重启应该做的事情......

答案 2 :(得分:0)

当我的compileSdkVersion为27且buildToolsVersion不是27时,我遇到了同样的问题。 将它们更改为     compileSdkVersion 27     buildToolsVersion“27.0.0” 我认为当buildToolsVersion比compileSdkVersion更旧时会发生这种情况。

答案 3 :(得分:-1)

我相信他们改变了findViewById的方法签名,这样你就不再需要演员了。尝试将该行代码更改为

Toolbar toolbar = findViewById(R.id.toolbar);
相关问题