蜂窝 ActionBar 项目 - 应用程序崩溃
我正在为设备 2.1 Eclair -> 开发 Android 应用程序3.0 蜂窝。我现在开始创建用户设置区域。我在“菜单”和“菜单”方面遇到了障碍。操作栏项目。
我创建了一个菜单 xml 资源,其中包含我的项目和图标,然后我将其连接到 java 中:我
在已有的内容之上添加了这些导入:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
然后在我的活动中将此代码作为 Android 开发者网站说:(
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.pixelappmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuItem_Help:
helpDialogGo();
return true;
case R.id.menuItem_Settings:
settingsActivityGo();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是我添加的全部内容,因为它工作正常。我甚至评论了所有这些新东西,它又开始工作了 当我在 Froyo上
测试它时,这有效 - 菜单显示了预期的标题和图标,但是当我尝试在我的 Honeycomb 平板电脑上启动该应用程序时,它强制关闭,甚至没有显示该应用程序进行拆分第二。
其他一些可能相关的代码:
中提取
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="11" />
从 androidmanifest.xml Pixelappmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Help"
android:id="@+id/menuItem_Help"/>
<item
android:title="Settings"
android:id="@+id/menuItem_Settings"
android:onClick="onMenuItemClick_Settings"/>
</menu>
I am developing an Android app for devices 2.1 Eclair -> 3.0 Honeycomb. I am now moving onto creating a user settings area. I have hit a snag with Menu & ActionBar Items.
I created a menu xml resource with my items and icons in it, then I hooked it up in java with this:
I added these imports on top of what I already had:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
And then this code in my activity as the Android Developers site said to:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.pixelappmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuItem_Help:
helpDialogGo();
return true;
case R.id.menuItem_Settings:
settingsActivityGo();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
(This is all I have added since it was working fine. I even commented all this new stuff out and it started working again on Honeycomb.)
This worked when I tested it on Froyo - the menu appeared with the titles and icons as its supposed to, but when I try to launch the app on my Honeycomb tablet, it force closes without even showing the app for a split second.
Some other possibly relevant code:
extract from androidmanifest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="11" />
pixelappmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Help"
android:id="@+id/menuItem_Help"/>
<item
android:title="Settings"
android:id="@+id/menuItem_Settings"
android:onClick="onMenuItemClick_Settings"/>
</menu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决办法。
在菜单 xml 文件(res/menu/mymenu.xml - 或任何您称为文件的文件)中,您需要声明允许菜单项出现在 ActionBar 上。您可以通过将
“在您的项目内”放置在 XML 中来完成此操作。
示例代码在这里:
I found a fix.
In the menu xml file (res/menu/mymenu.xml - or whatever you called your file), you need to declare that the menu item is allowed to be on the ActionBar. You do this by placing
Within your item in your XML.
Sample code is here: