2012-09-01 37 views
5

感謝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); 
    } 

} 

回答

0

插入目標API註釋只是上面覆蓋註釋

+0

謝謝,試過了,但仍然不適合我。 – Dave

+0

你可以發佈整個代碼,而不僅僅是這個部分。您使用的是哪種ADT版本? – nandeesh

+0

Eclipse for Mobile Juno構建ID:20120614-1722,Android開發工具20.0.3.v201208082019-427395。 – Dave

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(); 
} 
+0

謝謝 - 我試圖將它放在位置2時使用它,但我仍然無法使它工作。我在清單中設置了Min SDK版本11以獲取代碼進行編譯,但是我仍然想知道如何讓TargetApi與Min SDK版本8一起工作。您的代碼示例說@ Target,而不是@ TargetApi - 就是那個重大?無論我使用@ Target(11)還是@ TargetApi(11),我都會得到相同的錯誤(在代碼中爲「語法錯誤」,在方法之前「無法解析」)。 – Dave

+0

這是Android中的TargetApi註釋_itself_的源代碼,這不適用於您的Java代碼。它只是表明您只能在類型_declarations_,方法_declarations_或構造函數_declarations_上使用TargetApi註釋。 TargetApi註釋不允許在某些代碼塊的中間,因此您可以嘗試。 – Bananeweizen

相關問題