感謝P.T.看起來像是問題Building multi-SDK Android apps in Eclipse without losing compile-time checks的正確答案。但是,當我嘗試按照建議使用@TargetApi()註釋時,它會生成語法錯誤。Android Eclipse Lint API檢查
@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日---
如果我之前類定義(示出爲位置1)或方法的定義之前移動至@TargetApi註釋到(示出爲位置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);
}
}
謝謝,試過了,但仍然不適合我。 – Dave
你可以發佈整個代碼,而不僅僅是這個部分。您使用的是哪種ADT版本? – nandeesh
Eclipse for Mobile Juno構建ID:20120614-1722,Android開發工具20.0.3.v201208082019-427395。 – Dave