I support the Android app to support older devices (2.3.3+). It used to be 4.0+.
I use an ActionBar and configure it in code in a base class. After converting to ActionBar support, the style code is:
public static void styleActionBar(final ActionBarActivity activity) { final ActionBar actionBar = activity.getSupportActionBar(); if (actionBar != null) { actionBar.setBackgroundDrawable(new ColorDrawable(0xfffffff)); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); actionBar.setCustomView(R.layout.customview_actionbar); .... } }
After this change, everything works fine on 2.3.3. But, for 4.0+, it falls on the line
actionBar.setBackgroundDrawable(new ColorDrawable(0xfffffff));
Also, if I comment on this line, I see that it also fires on actionBar.setCustomView .
Stack Trace on 4.2:
.... Caused by: java.lang.NullPointerException at android.support.v7.app.ActionBarImplICS.setBackgroundDrawable(ActionBarImplICS.java:176) at com.mycompany.myproj.ProjBaseActivity.styleActionBar(ProjBaseActivity.java:68) at com.mycompany.myproj.ProjBaseActivity.onCreate(ProjBaseActivity.java:32) at com.mycompany.myproj.TextSearchActivity.onCreate(TextSearchActivity.java:60) at android.app.Activity.performCreate(Activity.java:5008) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
It works fine in 2.3.3, and it looks like a problem in support-v7-appcompat.
Any tips / help on overcoming or working? Thanks in advance.
Change contents 1: values-v14/styles.xml :
<resources> <style name="MyTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item> </style> <style name="MyTheme.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar"> <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> </style> <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@color/proj_theme_color</item> </style> </resources>
source share