4

(Android 3.0+) How would I create a Spinner for use as an Action Item for Android Honeycomb's Action Bar? I understand that the Action Bar's LIST mode pretty much does that, but I would like to use its TAB mode instead. Since, as far as I know, I can't have both on at the same time, I'm trying to use the spinner as an action item instead.

Here is the java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.spin_menu, menu);
    Spinner spin = (Spinner) findViewById(R.id.spin_widget);
    ArrayAdapter<CharSequence> spinAdaptor = ArrayAdapter.createFromResource(
         this, R.array.spinlist, android.R.layout.simple_spinner_item);
    spinAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(spinAdaptor);
    return super.onCreateOptionsMenu(menu);
}

No errors appear in eclipse, but running the program results in a force close. Any suggestions for a absolute beginner?

Update - Added logcat error severity log: (At least, that's what I think it is)

06-27 18:36:59.496: ERROR/AndroidRuntime(493): FATAL EXCEPTION: main
06-27 18:36:59.496: ERROR/AndroidRuntime(493): java.lang.NullPointerException
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at me.ics.icsActivity.onCreateOptionsMenu(icsActivity.java:84)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at android.app.Activity.onCreatePanelMenu(Activity.java:2389)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:347)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:647)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at com.android.internal.policy.impl.PhoneWindow$2.run(PhoneWindow.java:2581)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at android.os.Handler.handleCallback(Handler.java:587)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at android.os.Looper.loop(Looper.java:132)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at android.app.ActivityThread.main(ActivityThread.java:4025)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at java.lang.reflect.Method.invoke(Method.java:491)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-27 18:36:59.496: ERROR/AndroidRuntime(493): at dalvik.system.NativeStart.main(Native Method)

Jack
  • 43
  • 1
  • 4
  • Use `adb logcat`, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your "force close". – CommonsWare Jun 26 '11 at 23:16
  • @CommonsWare: I'm very new at this, but I think I found what you mentioned. The last log entry in the verbose view of the eclipse logcat is: `request time failed: java.net.SocketException: Address family not supported by protocol` It really seems to be something I'm doing wrong with the ArrayAdapter, as removing the .setAdapter line will make the program run without crashing, just with an empty spinner. – Jack Jun 27 '11 at 17:56
  • That has nothing to do with your issue, and it is not a stack trace. All force close dialogs result in a Java stack trace being written out at error severity to LogCat. – CommonsWare Jun 27 '11 at 18:00
  • @CommonsWare: I've added what I think is the logcat error severity information to the original question. Thank you for you patience; I am definitely receptive to any corrections needed. – Jack Jun 27 '11 at 18:53

2 Answers2

1

My guess is that you do not have a Spinner whose android:id is R.id.spin_widget.

If you are trying to put a Spinner as an action item, as your question states, you would not get that Spinner via findViewById(), but rather by getActionView() on the MenuItem in question. Here is a sample project demonstrating this, implemented in a way that will work on Honeycomb but also successfully skips this code on older versions of Android.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This, along with the sample project, helped me make this work, thanks! I believe I had been confusing the menu item with the spinner. I added a separate layout for the spinner, referenced it with the menu item, added `View v=menu.findItem(R.id.spin_item).getActionView();` and changed the spinner creation to reference off of v. I still don't know enough to say exactly why that worked, but it did. Thanks again! – Jack Jun 27 '11 at 20:02
0

I was looking for something like you want to do. This answer works me perfectly: https://stackoverflow.com/a/9118507/2374650

I do this:

my_action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:id="@+id/menu_spinner_pictures"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:entries="@array/types_view" />

</RelativeLayout>

MainPanelActivity.java

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Configure ActionBar        
        ActionBar actionBar = getActionBar();
        View spinner = getLayoutInflater().inflate(R.layout.my_action_bar, null);
        actionBar.setCustomView(spinner);
        actionBar.setDisplayShowCustomEnabled(true);
        (...)
Community
  • 1
  • 1
user8989
  • 1
  • 1