Sep 13, 2012

f Comment

Fixing Eclipse Android ADT 'Call Requires API Level 11' Error

Amazon I am using ADT plugin in Eclipse Indigo. The version of Android Development Tools I am using is 20.0.3.v201208082019-427395. When I follow the tutorial at http://developer.android.com/training/basics/firstapp/starting-activity.html I got the following error:

Call requires API level 11 (current min is 8): android.app.ActionBar#setDisplayHomeAsUpEnabled
The screen shot looks like this.

Eclipse Android SDK Call Requires API Level Error

This error occurs in onCreate() in DisplayMessageActivity.java. Why and How do I fix this error?

The Cause
This error is due to that you probably set your Android app to be able to run on API 8, which is Android 2.2. This is the default setting I had when I created a new Android App Project in Eclipse.

When you see this article there may be newer version of Android and your app may no longer run on this old version. But the fix should still apply.
In onCreate() in DisplayMessageActivity.java, the line getActionBar().setDisplayHomeAsUpEnabled(true); is not supported until API 11, or Android 3.0, or later. That's why you are getting this error.

Run 'Android Lint' in the toolbar to locate any Android related errors or warnings!
Below we will look at several possible solutions!

Solution #1: Change minSdkVersion!
Simply go to AndroidManifest.xml and change android:minSdkVersion="8" to the minimum version that supports the method in question, which in this case is android:minSdkVersion="11". Then clean the project and build again.

The downside to this solution is your app can no longer support Android SDK 8. If you'd like to market your Android app in Android marketplace this may be a concern.

Solution #2: Change Code to Support Older API
Change your Java code so that it works with the older API as well. For example you can use try-catch block as follows.
try{
getActionBar().setDisplayHomeAsUpEnabled(true);
}catch(Exception e){
//will not running getActionBar().setDisplayHomeAsUpEnabled(true); result in any errors? is this code critical?
}
A better way is detect the version of the Android version that the app is running on and act accordingly, as follows.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  // running platform is at least Honeycomb
  getActionBar().setDisplayHomeAsUpEnabled(true);
}
In general you should still support the older versions of Android to ensure maximum coverage of your app's consumers!

If you use @TargetApi(11) to avoid the error the error may be gone even after you remove @TargetApi(11) and clean and rebuild. It happened to me and I don't know why. If you can enlighten me that'd be great!
If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael