简体   繁体   English

Android Eclipse Lint API检查

[英]Android Eclipse Lint API checks

Thanks PT for what looks like the correct answer to question Building multi-SDK Android apps in Eclipse without losing compile-time checks . 感谢PT看起来正确的答案问题在Eclipse中构建多SDK Android应用程序而不会丢失编译时检查 However, when I try to use the @TargetApi() annotation as recommended, it generates syntax errors. 但是,当我尝试按照建议使用@TargetApi()注释时,会生成语法错误。

@TargetApi(11)    // location 1
public class DisplayMessageActivity extends Activity {

    @Override
    @TargetApi(11)    // location 2
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            @TargetApi(11)    // location 3
            getActionBar().setDisplayHomeAsUpEnabled(true); }

generates two syntax errors on the @TargetApi line when it is in the middle of the code as shown at location 3: @TargetApi行在代码中间生成两个语法错误,如位置3所示:

x Syntax error, insert "enum Identifier" to complete EnumHeaderName
x Syntax error, insert "enumBody" to complete BlockStatements

The errors exist whether I have the @TargetApi line before the if statement or after it as shown. 无论我在if语句之前还是之后都有@TargetApi行, @TargetApi出现错误。 Are there any prerequisites (imports) or other considerations not mentioned in the Lint API Check article http://tools.android.com/recent/lintapicheck to get @TargetApi() working correctly? 是否有任何先决条件(导入)或其他注意事项未在Lint API Check文章http://tools.android.com/recent/lintapicheck中提及以使@TargetApi()正常工作?

--- Edit 9/3/2012 --- ---编辑9/3/2012 ---

If I move the @TargetApi annotation to before the class definition (shown as location 1) or before the method definition (shown as location 2, either before or after the @Override annotation), I get different errors: 如果我将@TargetApi注释移动到类定义(显示为位置1)之前或方法定义之前(显示为位置2,在@Override注释之前或之后),我会得到不同的错误:

x TargetApi cannot be resolved to a type
x The attribute value is undefined for the annotation type TargetApi

--- Edit 9/4/2012 --- ---编辑9/4/2012 ---

Here is the full source code: 这是完整的源代码:

package com.example.my.first.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

    @TargetApi(11)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ActionBar introduced in Android 3.0 Honeycomb API 11
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true); }   // Up Navigation

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_display_message, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

FizzBuzz provided the answer to this in How do you version code in Android without compiler warnings? FizzBu​​zz 在没有编译器警告的情况下如何在Android版本代码中提供了这个答案 .

In addition to the @TargetApi(nn) annotation in the code, you also need to import the definition of that annotation: 除了代码中的@TargetApi(nn)注释之外,还需要导入该注释的定义:

import android.annotation.TargetApi;

For some unknown reason, an import is not required to use the @Override annotation. 由于某些未知原因,使用@Override注释不需要导入。 It would be helpful if the ADT documentation http://tools.android.com/recent/lintapicheck were fixed to eliminate the bogus code example and mention the required import. 如果修复ADT文档http://tools.android.com/recent/lintapicheck以消除虚假代码示例并提及所需的导入,将会很有帮助。

The example on the website using the annotation in the middle of the code is simply wrong (or maybe outdated). 网站上使用代码中间的注释的示例完全错误(或者可能已过时)。 The declaration of the annotation itself shows that it is only allowed for types, methods and constructors : 注释本身的声明表明它只允许类型,方法和构造函数

/** Indicates that Lint should treat this type as targeting a given API level, no matter what the
    project target is. */
@Target({TYPE, METHOD, CONSTRUCTOR})
@Retention(RetentionPolicy.CLASS)
public @interface TargetApi {
    /**
     * This sets the target api level for the type..
     */
    int value();
}

在覆盖注释的上方插入目标API注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM