【问题标题】:Android Eclipse Lint API checksAndroid Eclipse Lint API 检查
【发布时间】:2012-09-01 23:26:09
【问题描述】:

感谢 P.T.对于问题Building multi-SDK Android apps in Eclipse without losing compile-time checks 的正确答案。但是,当我尝试按照推荐使用 @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

无论我在if 语句之前还是之后有@TargetApi 行,都存在错误,如图所示。是否有任何先决条件(导入)或Lint API Check 文章http://tools.android.com/recent/lintapicheck 中未提及的其他注意事项以使@TargetApi() 正常工作?

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

如果我将 @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);
    }

}

【问题讨论】:

    标签: android eclipse android-lint


    【解决方案1】:

    FizzBu​​zz 在How do you version code in Android without compiler warnings? 中提供了答案。

    除了代码中的@TargetApi(nn)注解外,还需要导入该注解的定义:

    import android.annotation.TargetApi;
    

    由于某些未知原因,使用@Override 注释不需要导入。如果修正了 ADT 文档 http://tools.android.com/recent/lintapicheck 以消除虚假代码示例并提及所需的导入,将会很有帮助。

    【讨论】:

      【解决方案2】:

      网站上使用代码中间注释的示例是完全错误的(或者可能已过时)。注解本身的声明表明只允许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();
      }
      

      【讨论】:

      • 谢谢 - 我试图将它放在位置 2 的方法上使用,但我仍然无法让它工作。我已经在清单中设置了 Min SDK 版本 11 以获取要编译的代码,但我仍然想弄清楚如何让 TargetApi 与 Min SDK 版本 8 一起使用。您的代码示例说 @Target,而不是 @TargetApi - 是这样重要的?无论我使用@Target(11) 还是@TargetApi(11),我都会得到相同的错误(代码中的“语法错误”,方法之前的“无法解决”)。
      • 这是Android中TargetApi注解itself的源代码,不要在你的Java代码中使用。它只是表明您只能在类型 declarations、方法 declarations 或构造函数 declarations 处使用 TargetApi 注释。因此,在某些代码块的中间不允许使用 TargetApi 注释,但是您可以尝试。
      【解决方案3】:

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

      【讨论】:

      • 你能发布整个代码,而不仅仅是这部分。您使用的是哪个 ADT 版本?
      • Eclipse for Mobile Juno build id:20120614-1722,Android 开发工具 20.0.3.v201208082019-427395。
      • 在 Eclipse 窗口中 -> 首选项 -> android -> 检查忽略所有的 Lint 错误。然后删除 TargetAPi 注释。检查它是否有效
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多