Reputation: 5490
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:
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?getActionBar().setDisplayHomeAsUpEnabled(true);
statement is wrong in one activity, but perfectly fine in the other?Upvotes: 8
Views: 32091
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
Reputation: 115
just make a change in androidmanifest
as
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
Upvotes: 0
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
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)
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
Reputation: 641
Use getSupportActionBar()
instead of getActionBar()
. That will provide ActionBar features on API levels below 11.
Upvotes: 16
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
Upvotes: 14
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