I have an application which currently works on Android versions 4.x and more. I am trying to change it so it can be usable by older devices such as 2.x and more. Everything is working fine except the action bar. I am getting a null pointer exception every time I am trying to getSupportActionBar. This is am issue that have been discussed a lot in forums but I still cannot solve it.
The application uses the appCompat library but it does not seem to work on older devices.
styles.xml file
<style name="AppBaseTheme" parent="Theme.MyApp">
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
<item name="android:windowNoTitle">true</item>
</style>
MyApp.xml file
<style name="Theme.MyApp" parent="@style/Theme.AppCompat">
<item name="actionBarItemBackground">@drawable/selectable_background</item>
<item name="popupMenuStyle">@style/PopupMenu.MyApp</item>
<item name="dropDownListViewStyle">@style/DropDownListView.MyApp</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.MyApp</item>
<item name="actionDropDownStyle">@style/DropDownNav.MyApp</item>
<item name="actionBarStyle">@style/ActionBar.Transparent.MyApp</item>
<item name="actionModeBackground">@drawable/cab_background_top</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.MyApp</item>
Activity trying to get the actionbar
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auction_list);
if (android.os.Build.VERSION.SDK_INT >=11)
{
getActionBar().setIcon(R.drawable.gem);
getActionBar().setDisplayHomeAsUpEnabled(false);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
}
else
{
//This calls a private class inside the same class to get a supported action bar
notActionBar nab = new notActionBar();
nab.getNotActionBar(); //THIS IS THE ERROR LINE
}
Private class that gets the supported action bar. I have used a private class instead of directly getting the actionbar because i could not extend the ActionBarActivity because the activity all ready extends fragments.
private class notActionBar extends ActionBarActivity {
public void getNotActionBar(){
ActionBar actionBar = getSupportActionBar(); //NULL POINTER EXCEPTION
actionBar.setIcon(R.drawable.gem);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getSupportActionBar().setCustomView(v);
}
public void setTitleActionBar(String a){
getSupportActionBar().setTitle(a);
}
}
you should not try to instantiate Activities like this in Android:
notActionBar nab = new notActionBar();
the way to start Activities in Android is with startActivity(Intent intent)
or startActivityForResult method.
But anyhow, The solution for your Problem should be extending ActionBarActivity
instead FragmentActivity
in your main Activity.
You can do this also if you use Fragments! Because ActionBarActivity extends FragmentActivity
.