我如何创建一个 Spinner 来用作操作项?

发布于 2024-11-17 09:43:43 字数 2512 浏览 5 评论 0原文

(Android 3.0+) 我如何创建一个 Spinner 用作 Android Honeycomb 操作栏的操作项?我知道操作栏的 LIST 模式几乎可以做到这一点,但我想改用它的 TAB 模式。据我所知,因为我不能同时打开两者,所以我尝试使用微调器作为操作项。

这是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);
}

eclipse中没有出现错误,但是运行程序会导致强制关闭。对于绝对的初学者有什么建议吗?

更新 - 添加了 logcat 错误严重性日志:(至少,我是这么认为的)

06-27 18:36:59.496: 错误/AndroidRuntime(493): 致命异常:main
06-27 18:36:59.496:错误/AndroidRuntime(493):java.lang.NullPointerException
06-27 18:36:59.496:错误/AndroidRuntime(493):在me.ics.icsActivity.onCreateOptionsMenu(icsActivity.java:84)
06-27 18:36:59.496:错误/AndroidRuntime(493):在android.app.Activity.onCreatePanelMenu(Activity.java:2389)
06-27 18:36:59.496:错误/AndroidRuntime(493):在com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:347)
06-27 18:36:59.496: 错误/AndroidRuntime(493): 在 com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:647)
06-27 18:36:59.496:错误/AndroidRuntime(493):在com.android.internal.policy.impl.PhoneWindow$2.run(PhoneWindow.java:2581)
06-27 18:36:59.496:错误/AndroidRuntime(493):在android.os.Handler.handleCallback(Handler.java:587)
06-27 18:36:59.496:错误/AndroidRuntime(493):在android.os.Handler.dispatchMessage(Handler.java:92)
06-27 18:36:59.496:错误/AndroidRuntime(493):在android.os.Looper.loop(Looper.java:132)
06-27 18:36:59.496:错误/AndroidRuntime(493):在android.app.ActivityThread.main(ActivityThread.java:4025)
06-27 18:36:59.496:错误/AndroidRuntime(493):在java.lang.reflect.Method.invokeNative(本机方法)
06-27 18:36:59.496:错误/AndroidRuntime(493):在java.lang.reflect.Method.invoke(Method.java:491)
06-27 18:36:59.496: 错误/AndroidRuntime(493): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-27 18:36:59.496:错误/AndroidRuntime(493):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-27 18:36:59.496:错误/AndroidRuntime(493):在dalvik.system.NativeStart.main(本机方法)

(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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-11-24 09:43:43

我的猜测是,您没有 android:idR.id.spin_widgetSpinner

如果您尝试将 Spinner 作为操作项,正如您的问题所述,您将无法通过 findViewById() 获得该 Spinner,而是通过相关 MenuItem 上的 getActionView() 来实现。 这是一个示例项目演示了这一点,其实现方式如下可以在 Honeycomb 上运行,但也可以在旧版本的 Android 上成功跳过此代码。

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.

泪意 2024-11-24 09:43:43

我一直在寻找像你想做的事情。
这个答案对我来说非常有效:
https://stackoverflow.com/a/9118507/2374650

我这样做:

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

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);
        (...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文