我一直在关注 Android 官方培训计划,并且在类(class)管理 Activity 生命周期 » 启动 Activity 中,有一段代码是这样的:
public void onCreate(Bundle savedInstanceState) {
/* ... some other stuff ... */
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
然而,当我在我的测试应用程序中编写它时,我收到来自 ADT 的警告
Call requires API level 14 (current min is 11): android.app.ActionBar#setHomeButtonEnabled
我的应用程序确实是最低 API 级别 11,按照教程应该如此,但是当对该方法的调用在检查当前版本?培训计划继续让我相信这是可以的:
Caution: Using the SDK_INT to prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.
但是避免运行时错误并没有多大帮助,因为 ADT 甚至不允许我编译它。
最佳答案
but why is ADT acting so smug telling me this doesn't belong there, when the call to that method is inside a block that checks the current version
是的,你添加了它,但 IDE
对你正在通过 Build 检查版本一无所知。
只需添加 @SuppressLint("NewApi") 错误就会消失。或者您可以创建波纹管方法并在条件下使用它们:
@TargetApi(14)
private void actionAPI14() {
getActionBar().setHomeButtonEnabled(false);
}
private void action() {
getSupportActionBar().setHomeButtonEnabled(false);
}
您始终可以使用较低 API 版本的支持包。
关于android - 调用需要 API 级别 14(当前最低为 11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20298933/