Cảm ơn P.T. cho câu trả lời đúng cho câu hỏi Building multi-SDK Android apps in Eclipse without losing compile-time checks. Tuy nhiên, khi tôi cố gắng sử dụng chú thích @TargetApi() như được đề xuất, nó sẽ tạo ra các lỗi cú pháp.Kiểm tra API Lint Eclipse của Android
@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); }
tạo ra hai lỗi cú pháp trên dòng @TargetApi khi nó đang ở giữa của mã như hiển thị ở vị trí 3:
x Syntax error, insert "enum Identifier" to complete EnumHeaderName
x Syntax error, insert "enumBody" to complete BlockStatements
Các lỗi tồn tại cho dù tôi có dòng @TargetApi
trước if
tuyên bố hoặc sau khi nó được hiển thị. Có bất kỳ điều kiện tiên quyết (nhập khẩu) hoặc cân nhắc khác không được đề cập trong bài viết Lint API Check
http://tools.android.com/recent/lintapicheck để nhận @TargetApi() hoạt động chính xác không?
--- Sửa 2012/09/03 ---
Nếu tôi di chuyển chú thích @TargetApi tới trước khi định nghĩa lớp (hiển thị như vị trí 1) hoặc trước khi định nghĩa phương pháp (hiển thị như vị trí 2, hoặc trước hoặc sau khi chú thích @ Override), tôi nhận được lỗi khác nhau:
x TargetApi cannot be resolved to a type
x The attribute value is undefined for the annotation type TargetApi
--- Sửa 2012/09/04 ---
đây là mã nguồn đầy đủ:
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);
}
}
Cảm ơn, đã thử nhưng vẫn không hoạt động đối với tôi. – Dave
bạn có thể đăng toàn bộ mã, không chỉ phần này. Bạn đang sử dụng phiên bản ADT nào? – nandeesh
Eclipse cho Id xây dựng Juno dành cho thiết bị di động: 20120614-1722, công cụ phát triển Android 20.0.3.v201208082019-427395. – Dave