谢谢 P.T.看起来像是问题的正确答案在 Eclipse 中构建多 SDK Android 应用程序而不会丢失编译时检查 https://stackoverflow.com/questions/7642249。但是,当我尝试按照建议使用 @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 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);
}
}