我如何创建一个 Spinner 来用作操作项?
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,您没有
android:id
为R.id.spin_widget
的Spinner
。如果您尝试将
Spinner
作为操作项,正如您的问题所述,您将无法通过findViewById()
获得该Spinner
,而是通过相关MenuItem
上的getActionView()
来实现。 这是一个示例项目演示了这一点,其实现方式如下可以在 Honeycomb 上运行,但也可以在旧版本的 Android 上成功跳过此代码。My guess is that you do not have a
Spinner
whoseandroid:id
isR.id.spin_widget
.If you are trying to put a
Spinner
as an action item, as your question states, you would not get thatSpinner
viafindViewById()
, but rather bygetActionView()
on theMenuItem
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.我一直在寻找像你想做的事情。
这个答案对我来说非常有效:
https://stackoverflow.com/a/9118507/2374650
我这样做:
my_action_bar.xml
MainPanelActivity.java
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
MainPanelActivity.java