Android Eclipse Lint API检查 [英] Android Eclipse Lint API checks

查看:335
本文介绍了Android Eclipse Lint API检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢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语法错误,插入枚举标识符完成EnumHeaderName 
x语法错误,插入enumBody 完成BlockStatements

无论我是否有 @TargetApi 之前,如果语句或之后显示。是否有任何先决条件(导入)或其他未在 Lint API检查中提及的注意事项文章 http://tools.android.com/recent/lintapicheck 获取@TargetApi()是否正常工作?



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



如果我将@TargetApi注释移动到类定义之前(如位置1所示)或方法定义之前(显示)作为位置2,在@Override注释之前或之后),我收到不同的错误:

  x TargetApi无法解析为键入
x注释类型的属性值未定义TargetApi

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



以下是完整的源代码:

  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);
//在Android 3.0中引入的ActionBar Honeycomb API 11
if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.HONEYCOMB){
getActionBar()。setDisplayHomeAsUpEnabled(true); } //向上导航

//从意图获取消息
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

//创建文本视图
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

//将文本视图设置为活动布局
setContentView(textView);
}

@Override
public boolean onCreateOptionsMenu(菜单菜单){
getMenuInflater()。inflate(R.menu.activity_display_message,menu);
返回true;
}


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

}


解决方案

FizzBu​​zz在如何做出答案你的版本代码在Android中没有编译器警告?



除了 @TargetApi(nn)

  import android.annotation.TargetApi; 

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


Thanks P.T. for what looks like the correct answer to question Building multi-SDK Android apps in Eclipse without losing compile-time checks. However, when I try to use the @TargetApi() annotation as recommended, it generates syntax errors.

@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:

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. 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?

--- Edit 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:

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

--- Edit 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?.

In addition to the @TargetApi(nn) annotation in the code, you also need to import the definition of that annotation:

import android.annotation.TargetApi;

For some unknown reason, an import is not required to use the @Override annotation. 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.

这篇关于Android Eclipse Lint API检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆