android - 调用需要 API 级别 14(当前最低为 11)

标签 android adt

我一直在关注 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/

相关文章:

Android Auto 应用从不调用 onGetRoot

android - 可绘制图像未按预期在平板电脑上重新缩放

java - 无法解析方法 'assertThat(int)'

android - 如何在 Debug模式下逐步执行 Eclipse ADT

java - 如何为单选按钮分配一个值并稍后在 Android 中检索该值?

Android ADT 在 Eclipse Neon.2 中无法识别

android - adb devices 命令不显示任何内容

android - 约束 View 内的 ListView 被剪切并且不滚动

android - 将 SDK 工具和 ADT 更新为 17,现在出现 VerifyErrors

android - eclipse classic 4.2.1 - 从 android sdk 开始