Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck with @SuppressLint("NewApi") (developer.android.com/training/basics/firstapp/starting-activity)

Tags:

android

I'm a begginer with Android and currently stuck with the lession: http://developer.android.com/training/basics/firstapp/starting-activity.html

In the part Create the Second Activity, when I try to use the code:

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I get the error below:

@SuppressLint("NewApi") -> The attribute value is undefined for the annotation type SuppressLint.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) -> HONEYCOMB cannot be resolved or is not a field.

getActionBar().setDisplayHomeAsUpEnabled(true); -> The method getActionBar() is undefined for the type DisplayMessageActivity.

NavUtils.navigateUpFromSameTask(this); -> NavUtils cannot be resolved

Someone let me know how to solve? Here is what I imported:

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;

Many thanks!

like image 711
Lạng Hoàng Avatar asked Feb 01 '13 09:02

Lạng Hoàng


3 Answers

SuppressLint annotation was added in API level 16. You need to either:

  • set your build SDK to 16 or higher, or
  • copy tools/support/annotations.jar from your Android SDK to the project libs.
like image 100
laalto Avatar answered Nov 15 '22 13:11

laalto


import android.annotation.SuppressLint;

like image 29
Klaasel Avatar answered Nov 15 '22 15:11

Klaasel


I ran into the exact same problem. CTRL-SHIFT-o (organize imports) filled in the missing import for me.

like image 38
steamPickle Avatar answered Nov 15 '22 13:11

steamPickle