0

My app runs great on the ICS emu. However, using Gingerbread, it crashes whenever my onCreateOptionsMenu is in use. It also throws another, similar but different set of errors if I use showPopup instead. Does anyone know what exactly is going on here? Ideally, I would like to have one bit of menu code (excluding the listener, of course) that will lay out a menu for all versions of android running the app. I should mention, though, that I have a GUI menu button (sitting in my XML file as an ImageView), rather than one in the ActionBar. So, here's the code:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;

}

Here's the menu XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/demographics"
              android:title="@string/demographics" />
        <item android:id="@+id/tabSelection"
              android:title="@string/tabs" />            
        <item android:id="@+id/settings"
              android:title="@string/settings" />


</menu>

And here's the LogCat:

threadid=1: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: main
java.lang.IllegalStateException: Cannot interact with object designed for temporary instance passing. Make sure you using both SherlockFragmentActivity and SherlockFragment.
at com.actionbarsherlock.internal.view.menu.MenuMule.add(MenuMule.java:40)
at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:310)
at android.view.MenuInflater.parseMenu(MenuInflater.java:154)
at android.view.MenuInflater.inflate(MenuInflater.java:80)
at com.davekelley.polling.Polling.onCreateOptionsMenu(Polling.java:203)
at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:209)
at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:236)
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:543)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:467)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:269)
at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(ActionBarSherlockCompat.java:972)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Davek804
  • 2,804
  • 4
  • 26
  • 55

2 Answers2

1

Per this answer, You need to add in v4 support.

Community
  • 1
  • 1
Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • Definitely using Compat Lib already! I'll try to get it working with these solutions, but I had already tried them out to no avail. It seems there is some incongruity with ABS when it comes to getting the Menus to run on older devices (mixing standard android libs, compat lib and ABS seems to be rather complex)! – Davek804 May 08 '12 at 01:46
1

So basically in order to complete this, I can't use showPopup because that's only on API 11 and up.

I had so much trouble trying to get it all to work properly on the older versions of Android due, at least partially, to some incongruities with ActionBarSherlock that I didn't fully understand. Which imports I used were very important to getting the app to launch without crashes, here they are:

import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem;

Beyond that, I found another Stack Overflow post that highlighted the method openOptionsMenu. So in my fragment, I add an onClickListener for my menuButton ImageView. When the user taps that, the main activity is told to openOptionsMenu, which runs onCreateOptionsMenu. Right now the onMenuItemClick method doesn't seem to be doing its job, but I think I'll be able to sort that out fairly quickly tomorrow. There is one difference to how the menu used to load now though. Rather than popping up right on top of the ImageView, it loads on the bottom of the screen (in either the old way, or the new vertical menu in ICS). So there's that, but it's not a big problem.

I think that just about covers it.

Code:

    ImageView menuImg = (ImageView) activity.findViewById(R.id.menuImageView);
    menuImg.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            activity.openOptionsMenu();
        }
    });

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;    
}

public boolean onMenuItemClick(MenuItem item) {
     switch (item.getItemId()) {
        case R.id.demographics:

            return true;
        case R.id.settings:
            Log.v("v", "settings clicked");
            return true;
        default:
            return false;
     }
}
Davek804
  • 2,804
  • 4
  • 26
  • 55