谢谢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
如果我在@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
---编辑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);
// 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);
}
}
答案 0 :(得分:9)
FizzBuzz在How do you version code in Android without compiler warnings?中提供了答案。
除了代码中的@TargetApi(nn)
注释外,您还需要导入该注释的定义:
import android.annotation.TargetApi;
由于某些未知原因,使用@Override
注释不需要导入。如果修复ADT文档http://tools.android.com/recent/lintapicheck以消除虚假代码示例并提及所需的导入,将会很有帮助。
答案 1 :(得分: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 :(得分:0)
在覆盖注释
之上插入目标API注释