1

I am using Actionbar in my App, it working fine below api 22.

using android.support.v4.app.FragmentActivity

use below code for Action bar

public void initActionBar() {
           actionBar = getActionBar();
           actionBar.setDisplayShowTitleEnabled(true);
           actionBar.setDisplayHomeAsUpEnabled(false);
           actionBar.setDisplayUseLogoEnabled(false);
           actionBar.setHomeButtonEnabled(true);
           actionBar.setTitle("Rides");
           actionBar.setIcon(R.drawable.menu);
           actionBar.setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.orange)));
           actionBar.setDisplayShowCustomEnabled(true);
}

App Theme in style.xml

<!-- Application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:activatedBackgroundIndicator">@drawable/list_activated_background</item>
    <item name="android:typeface">serif</item>
    <item name="android:actionBarSize">55dip</item>
</style>

When run same In version 22 get below error

LogCat Error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference at com.holachef.android.chef_android.activity.HomeActivity.initActionBar(HomeActivity.java:160) at com.holachef.android.chef_android.activity.HomeActivity.onCreate(HomeActivity.java:62) at android.app.Activity.performCreate(Activity.java:5953) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135)

use android studio for this, same code in eclips run successfully on version 22, Is issue related with android studio.

Any help will be appreciated. Thanks.

Lokesh
  • 3,247
  • 2
  • 33
  • 62

2 Answers2

1

You must extend AppCompatActivity instead of FragmentActivity to have Actionbar with fragments.

If you're using the v7 appcompat library, your activity should instead extend AppCompatActivity, which is a subclass of FragmentActivity (for more information, read Adding the Action Bar).

Still you can try this,

ActionBar actionBar = getSupportActionBar();

More detail you can found here. http://developer.android.com/training/basics/fragments/creating.html

And please switch to Material Theme rather than using the old Holo Theme.

capt.swag
  • 10,335
  • 2
  • 41
  • 41
0

For Android Studio, the support libraries are all handled by Gradle now. So go to this file:

enter image description here

Now pay attention to the parts I highlighted in red. Try playing around withe the "minSdkVersion" and rebuild gradle. enter image description here

Gene
  • 10,819
  • 1
  • 66
  • 58