android的findviewbyid,Android O预览findViewById编译错误
我試圖測試
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
findViewById(int) in AppCompatActivity match
where T is a type-variable:
T extends View declared in method 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)中聲明的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版.
總結
以上是生活随笔為你收集整理的android的findviewbyid,Android O预览findViewById编译错误的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 宜家将于 4 月推出便携式蓝牙音箱 VA
 - 下一篇: 《星空》定级18+