Java 理解@SuppressLint("NewApi") 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16601601/
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-16 12:23:34  来源:igfitidea点击:

Understanding @SuppressLint("NewApi") annotation

javaandroideclipseandroid-lint

提问by vhd

I am an android beginner. While trying a code of managing activity life cycle, I encountered a new thing.

我是安卓初学者。在尝试管理活动生命周期的代码时,我遇到了一个新事物。

package com.example.activitylaunch;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.text_message);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
    }
    }

@Override
public void onDestroy(){
    super.onDestroy();
    android.os.Debug.stopMethodTracing();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

I understood the code well, but it gave an error in ActionBar SuppressLint. When I double clicked it, @SuppressLint("NewApi")is being added. What is meant by @SuppressLint("NewApi")here?

我很好地理解了代码,但是它在 ActionBar SuppressLint 中出现了错误。当我双击它时,@SuppressLint("NewApi")正在添加。这里是什么意思@SuppressLint("NewApi")

采纳答案by Raghav Sood

@SuppressLint("NewApi")is an annotation used by the Android Lint tool.

@SuppressLint("NewApi")是 Android Lint 工具使用的注释。

Lint will tell you whenever something in your code isn't optimal or may crash. By passing NewApithere, you're suppressing all warnings that would tell you if you're using any API introduced after your minSdkVersion

每当您的代码中的某些内容不是最佳的或可能崩溃时,Lint 都会告诉您。通过NewApi那里,您将抑制所有警告,这些警告会告诉您是否正在使用在您之后引入的任何 APIminSdkVersion

See a full list of Lint checks - including "NewApi" - here: http://tools.android.com/tips/lint-checks

查看完整的 Lint 检查列表 - 包括“NewApi” - 在这里:http://tools.android.com/tips/lint-checks

回答by Kiwi

Source: click here

来源:点击这里

In addition to testing that your Android application meets its functional requirements, it's important to ensure that your code has no structural problems. Poorly structured code can impact the reliability and efficiency of your Android apps and make your code harder to maintain. For example, if your XML resource files contain unused namespaces, this takes up space and incurs unnecessary processing. Other structural issues, such as use of deprecated elements or API calls that are not supported by the target API versions, might lead to code failing to run correctly.

除了测试您的 Android 应用程序是否满足其功能要求之外,确保您的代码没有结构问题也很重要。结构不佳的代码会影响 Android 应用的可靠性和效率,并使代码更难维护。例如,如果您的 XML 资源文件包含未使用的命名空间,这会占用空间并导致不必要的处理。其他结构性问题,例如使用不推荐使用的元素或目标 API 版本不支持的 API 调用,可能会导致代码无法正确运行。

The Android SDK provides a code scanning tool called lintthat can help you to easily identify and correct problems with the structural quality of your code, without having to execute the app or write any test cases. Each problem detected by the tool is reported with a description message and a severity level, so that you can quickly prioritize the critical improvements that need to be made. You can also configure a problem's severity level to ignore issues that are not relevant for your project, or raise the severity level. The tool has a command-line interface, so you can easily integrate it into your automated testing process.

Android SDK 提供了一个名为的代码扫描工具lint,可以帮助您轻松识别和纠正代码结构质量方面的问题,而无需执行应用程序或编写任何测试用例。该工具检测到的每个问题都会报告一个描述消息和一个严重性级别,以便您可以快速确定需要进行的关键改进的优先级。您还可以配置问题的严重性级别以忽略与您的项目无关的问题,或提高严重性级别。该工具具有命令行界面,因此您可以轻松地将其集成到您的自动化测试过程中。

The linttool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. You can run lintfrom the command-line or from the Eclipse environment.

lint工具会检查您的 Android 项目源文件中是否存在潜在错误,并在正确性、安全性、性能、可用​​性、可访问性和国际化方面进行优化改进。您可以从命令行lint或 Eclipse 环境中运行。