OnCreateOptionsMenu crash below API level 16

312 views Asked by At

I've been building an app (my first) from the Master/Detail flow template in eclipse. I've added actionbarsherlock to the project successfully and the app will run down to API 10, though whenever I try to call mItem.id (the ID of the list item selected) from onCreateOptionsMenu() in my detail fragment on anything less than API 16 (android 4.1) mItem.id is null. API 17 & 18 have no problem and seem to declare the item id before the action bar.

The fragment class

public class ModuleDetailFragment extends SherlockFragment implements OnClickListener {
    //The fragment argument representing the item ID that this fragment represents.
    public static final String ARG_ITEM_ID = "item_id";
    // The content this fragment is presenting.
    public ModuleList.ModuleItem mItem;

and onCreateOptionsMenu:

@Override                                   
public void onCreateOptionsMenu(Menu menu, MenuInflater mi) {
    Toast.makeText(getActivity(), "item id = "+mItem.id, Toast.LENGTH_SHORT).show();
}

I have have if statements identifying the button pressed and displaying the correct actionbar menu items, but this is enough to cause the crash.

Any pointers would be much appreciated, google has thus far returned very little.

Regards,

Dan

1

There are 1 answers

0
staticlife On

Eventually figured it out, was able to query the build version and re-initialise the menu bar after declaring the variables in pre Jellybean APIs with getActivity().invalidateOptionsMenu();

Probably not the most efficient solution but it's working for me (for honeycomb upwards anyway).