11

我尝试测试 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();
        }
    });
}

=> 编译错误

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

这不是铸造问题。

我的 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

4 回答 4

2

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

尝试同步 gradle 和 Build- >Clean Project。如果这没有帮助,File-> Invalidate Cache / Restart应该做的事情......

于 2017-06-05T16:00:27.590 回答
1

的方法签名findViewById随着 API-Level 25 的引入而改变,以支持泛型并移除丑陋的强制转换:

新方法签名:

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

与旧的相比:

public View findViewById(int id);

因此将您的代码更改为:

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

参考:查看|Android开发者

于 2017-05-23T06:09:31.913 回答
0

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

于 2018-01-22T12:26:37.587 回答
-1

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

Toolbar toolbar = findViewById(R.id.toolbar);
于 2017-05-23T05:59:34.017 回答