2012-09-01 32 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

相关问题