Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling error with API 10

I was following the basic tutorials on developers.android.com and came by creating the activity named DisplayMessageActivity. It is a blank activity with all the specifications given as shown in the tutorial. FYI, I am using Min SDK = API 8, Target SDK = API 16, Compile with = API 10

The next thing is that there are two errors:

  1. "The method getActionBar() is undefined for the type DisplayMessageActivity
  2. "home cannot be resolved or is not a field"

I tried changing the API to 14 which called for another problem, it wants the minimum API to be 11.

That solves these problems, but the main problem is so many devices still use Gingerbread or maybe FroYo. Can't I write for them? Do I have to go higher? How to write for them?

like image 290
Suhel Chakraborty Avatar asked Dec 09 '12 03:12

Suhel Chakraborty


1 Answers

First, you're going to want to make sure that you are compiling against the latest version of Android. You should update your sdk version because you're compiling for API 10 but targeting 16. Stuff may break if you do that, so it's best to stay updated to be safe. This means right clicking on your project in Eclipse, clicking on Properties then clicking on Android. Check off the highest version API that is there. If you have the latest version, it is Android 4.2. Then in your AndroidManifest.xml, set the android:targetSdkVersion to what you chose (my case api 17).

enter image description here

This should ensure that your app can run on basically HoneyComb to JellyBean. However, this app wants to run on at least froyo. This next part will allow your app to run on all devices.

Make a method like this:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

It checks to see which API it is running on and only if it is honeycomb and above, enables the action bar. Call it from onCreate() You will need to take out the getActionBarCall that is in onCreate() as it is not needed anymore there.

As for home not being enabled, it might have just been a wrong project target or you forgot to write android.R.id.home and instead wrote R.id.home.

Lastly with all these changes you should clean your project (Project -> Clean).

like image 80
A--C Avatar answered Sep 22 '22 14:09

A--C