开发者

How would I create a Spinner for use as an Action Item?

开发者 https://www.devze.com 2023-03-15 08:07 出处:网络
(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 i

(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 开发者_Python百科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)


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.


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);
        (...)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号