David Cary
David Cary

Reputation: 5490

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:

Upvotes: 8

Views: 32091

Answers (7)

David
David

Reputation: 1664

I usually fix it with "@TargetApi(12)" annotation

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

Upvotes: 3

Om R Kattimani
Om R Kattimani

Reputation: 115

just make a change in androidmanifest

as

<uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

Upvotes: 0

Syed Fahad Ali
Syed Fahad Ali

Reputation: 59

issue is when you create an android app in eclipsyou have asked to select min and max api support and by default eclips select 8 as min and 19 as max if you encounter above error means the function you are using is of version which is greater then 8
to resolve this issue i just go in AndroidManifest.xml file and change following

<uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

minsdkversion by default is 8

Upvotes: -2

drdrej
drdrej

Reputation: 908

if you use appcompat-v7 and higher you can't use actionbarsherlock because both have same attributes defined. so this libraries have a conflict in resources.

to do it with support library you need: (i hope you've allready downloaded and integrated the support library in your project)

  1. you must use ActionBar from the support library, so you have to extends your activity with the right ActionBarActivity implementation.

imports:

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;

activity:

public class PlacesActivity extends ActionBarActivity {

    @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
}

so you can get the actionBar implemenation from Activity supported by older versions:

 ActionBar actionBar = activity.getSupportActionBar();

write code & have fun!

Upvotes: 4

Bal&#225;zs Palk&#243;
Bal&#225;zs Palk&#243;

Reputation: 641

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

Upvotes: 16

Simon-Droid
Simon-Droid

Reputation: 409

In short, Action Bar is not available in older versions of Android, and google's support library doesn't provide that feature either. This is the reason why lint is complaining - you are using code that doesn't exist in API verison 8 which is set as a minimum API level your app supports. If you want to use action bar on older android versions, use open source library called "Action Bar Sherlock".

Now, to answer your questions

  1. This should be an error in both classes. The reason why it's not an error in one of the classes is not actually important.
  2. No
  3. It's wrong in both activities
  4. You have to use open source library that provides action bar on older android versions, i.e. ActionBar Sherlock works great for this purpose
  5. The really right thing to do is to open Android Developers website and read about the limitations of support library.

Upvotes: 14

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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?

Just edit the second file and save. It will give error!

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?

May be

Is maybe there a bug in Eclipse Lint, and that statement is actually OK in both activities (or perhaps wrong in both activities)?

both wrong in given context

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.

You need to change minSDK I guess no other option. If you want to use Functions API 11

Upvotes: 1

Related Questions