Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Call requires API level 11" with Android Activity?

I'm just starting to do Android development, more-or-less following the http://developer.android.com/training/basics/firstapp/starting-activity.html tutorial, and one thing keeps bugging me: I have two classes, both created using the "Android Activity" creation wizard:

public class PlacesActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places);
        // I manually commented out the following line to get it to compile.
        // getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    // ... other methods
}

and

public class ShowOnePlace extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_one_place);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    // ... other methods
}

Eclipse kept giving me the following errors:

Call requires API level 11 (current min is 8): android.app.Activity#getActionBar

Call requires API level 11 (current min is 8): android.app.ActionBar#setDisplayHomeAsUpEnabled

until I commented out one of the lines (as shown above). Now the application seems to compile and run on my Android device without errors. (Well, at least without that error).

So:

  • Why is getActionBar().setDisplayHomeAsUpEnabled(true); give that error in one class, but that exact same line is not an error -- not even a warning -- in the other class?
  • Is there a bug in the "Android Activity" creation wizard, and somehow that getActionBar().setDisplayHomeAsUpEnabled(true); statement is wrong in one activity, but perfectly fine in the other?
  • Is maybe there a bug in Eclipse Lint, and that statement is actually OK in both activities (or perhaps wrong in both activities)?
  • What do I have to do so that I can fix this without changing minSdkVersion="8" to minSdkVersion="11"? I was under the impression that the "android-support-v4.jar" file was a compatibility library that ran on version 8 devices to handle stuff that's handled natively in version 11 devices.
  • What's really the Right Thing to Do when I see this error?
like image 577
David Cary Avatar asked Nov 30 '12 22:11

David Cary


People also ask

What API level should I use Android?

When you upload an APK, it must meet Google Play's target API level requirements. New apps must target Android 12 (API level 31) or higher; except for Wear OS apps, which must target Android 11 (API level 30) or higher.

Can we change API level in Android Studio?

Step 1: Open your project in Android mode then go to Gradle Scripts > build. gradle(Module: app) as shown in the following image. Step 2: Refer to the below image and here you have to change the minSdkVersion and targetSdkVersion as per the requirement.

What is Android API used for?

Uses of API Level in Android. The API Level identifier serves a key role in ensuring the best possible experience for users and application developers: It lets the Android platform describe the maximum framework API revision that it supports. It lets applications describe the framework API revision that they require.


1 Answers

Use getSupportActionBar() instead of getActionBar(). That will provide ActionBar features on API levels below 11.

like image 196
Balázs Palkó Avatar answered Sep 28 '22 20:09

Balázs Palkó