安卓 Eclipse Lint API 检查

5

感谢P.T.为在Eclipse中构建多SDK Android应用程序而不失去编译时检查问题提供了正确的答案。然而,当我尝试像推荐的那样使用@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); }

当@TargetApi行位于代码中间时,会生成两个语法错误,如位置3所示:

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

无论我将@TargetApi行放在if语句前还是后,错误都存在。除了Lint API Check文章http://tools.android.com/recent/lintapicheck中提到的内容之外,是否还有其他先决条件(导入)或其他考虑因素可以使@TargetApi()正常工作?
--- 编辑 9/3/2012 ---
如果我将@TargetApi注释移到类定义之前(显示为位置1)或方法定义之前(显示为位置2,即在@Override注释之前或之后),我会得到不同的错误:
x TargetApi cannot be resolved to a type
x The attribute value is undefined for the annotation type TargetApi

--- 编辑 2012年9月4日 ---

以下是完整的源代码:

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);
    }

}
3个回答

9

2
网站上使用代码中间的注释示例是错误的(或许已过时)。注释本身的声明表明它仅适用于类型、方法和构造函数。请参考Java文档
/** 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();
}

谢谢 - 我正在尝试在位置2处使用它,但我仍然无法让它工作。我已经在清单中设置了Min SDK版本11以使代码编译,但我仍想弄清楚如何在Min SDK版本8中使用TargetApi。您的代码示例说@ Target而不是@ TargetApi - 这有意义吗?无论我是使用@ Target(11)还是@ TargetApi(11),我都会收到相同的错误(当它在代码中时为“语法错误”,当它在方法之前时为“无法解析”)。 - Dave
这是Android中TargetApi注解本身的源代码,不应在您的Java代码中使用。它只是表明您只能在类型声明、方法声明或构造函数声明处使用TargetApi注解。因此,在某些代码块中间是不允许使用TargetApi注解的,无论您如何尝试。 - Bananeweizen

0

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


你能发布整个代码吗,而不仅仅是这一部分?你使用的是哪个 ADT 版本? - nandeesh
Eclipse for Mobile Juno版本号为20120614-1722,Android开发工具版本号为20.0.3.v201208082019-427395。 - Dave
在Eclipse中,依次选择“Window”->“Preferences”->“android”->“Lint Error Checking”,然后将其设置为“Ignore All”。接着移除TargetApi注释。检查是否生效。 - nandeesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接