Android 如何删除“调用需要 API 级别”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12631348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:15:32  来源:igfitidea点击:

How to remove "Call requires API level" error?

androideclipse

提问by BArtWell

I get this error in Eclipse: Call requires API level 14 (current min is 8): android.app.ActionBar#setHomeButtonEnabled

我在 Eclipse 中收到此错误:Call requires API level 14 (current min is 8): android.app.ActionBar#setHomeButtonEnabled

This is code:

这是代码:

if(android.os.Build.VERSION.SDK_INT>=14) {
    getActionBar().setHomeButtonEnabled(false);
}

In Manifest:

在清单中:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" />

How to remove this error?

如何消除这个错误?

回答by Eric

Add the line @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)above the method signature, where Build.VERSION_CODES.ICE_CREAM_SANDWICHevaluates to 14, the API version code for Ice Cream Sandwich.

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)在方法签名上方添加一行,其中的Build.VERSION_CODES.ICE_CREAM_SANDWICH计算结果为 14,即 Ice Cream Sandwich 的 API 版本代码。

Like so:

像这样:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void yourMethod() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        getActionBar().setHomeButtonEnabled(false);
    }
}

回答by TmTron

Note: the accepted answeris outdated.

注意:接受的答案已过时。

In Android Studio 3.0 Beta 7you don't need the @TargetApiannotation anymore.
It seems that the lint check is smarter now.

Android Studio 3.0 Beta 7您不再需要@TargetApi注释。
现在看来 lint 检查更智能了。

So this is enough:

所以这就足够了:

public void yourMethod() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        getActionBar().setHomeButtonEnabled(false);
    }
}