我尝试测试 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 版。