Wednesday, November 21, 2012

getActionBar and targetSdkVersion in Android development

Hi All,

Recently I was trying to reiterate Android docs training just to fill in anything I missed previously. There I came across this piece of code:

     // Initialize member TextView so we can manipulate it later  
     mTextView = (TextView) findViewById(R.id.edit_message);  
     // Make sure we're running on Honeycomb or higher to use ActionBar APIs  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
       // For the main activity, make sure the app icon in the action bar  
       // does not behave as a button  
       ActionBar actionBar = getActionBar();  
       actionBar.setHomeButtonEnabled(false);  
     }  


I tried to put that in a simple hello world example and it worked on an 2.2 emulator, however it failed to run on 4.0 emulator. On debugging it turned out that getActionBar was returning null value. Though there was on-line help available for some other directions it just came to my mind to check AndroidManifest.xml file. There I saw only "android:minSdkVersion="8"" I remembered that there was a target SDK  as well which can be defined. So I checked a project created by eclipse and found the "android:targetSdkVersion="15"" line and added it to "uses-sdk " tag in AndroidManifest.xml and the app started working on 4.0 emulator.

This clears two things, first is that Idea IDE adds only minSDKVersion while eclipse adds targetSDKVersion as well. Which is reflected in new project also but could effect like this.

Second, if you search for getActionBar returning null you get loads of advice but maybe not this. So check if you are having this issue.

Thanks for reading. Thanks for Android guys for creating such a good documentation, which by and large so much better than Facebook developer docs. Only wish Facebook docs were also as clear as the Android docs.

12 comments:

  1. Thanks for this programming tutorial. Yes Android has a good documentation

    ReplyDelete
  2. Well great post you have written for android OS. Thanks
    Mobile Apps Development india

    ReplyDelete
  3. hey, thanks for providing this code to us..well written post.
    technical consulting services

    ReplyDelete
  4. hey admin .. Nice post about Android development and I m also thankful to you for providing this programming script.. keep sharing..!!!

    IT Consulting Services

    ReplyDelete
  5. Never known before, looks like some useful Android functions

    ReplyDelete
  6. Tutorials written by you are awesome, they are easy to understand as well as easy to implement. just a piece of code implemented on my app.
    http://way2smshub.com/way2sms-mobile-apps/

    ReplyDelete
  7. Congrats for making it possible for creating this blog. I may not always visit this blog to see more updates but i will consider every valuable information i read here. Please keep up the good work and good luck for the upcoming post.

    Way2sms login

    ReplyDelete
  8. Congrats for making it possible for creating this blog. I may not always visit this blog to see more updates but i will consider every valuable information i read here. Please keep up the good work and good luck for the upcoming post.

    Way2sms login

    ReplyDelete
  9. Thanks for sharing this code with us. It is really helpful for developers..
    web application development service

    ReplyDelete