添加菜单到Android应用程序

时间:2013-04-21 13:19:14

标签: android menu

我一直在尝试将一个菜单添加到我的Android应用程序中,但它无法正常工作。当我单击“菜单”按钮时,应用程序停止工作。 这是我的options_menu.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

  <item 
    android:title="Profile"
    android:id="@+id/menu_profile"/>

  <item 
    android:title="About"
    android:id="@+id/menu_about"/>

</menu>

这是我的OptionsMenu.java类:

package com.carpool.jad;

import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuInflater;

public class OptionsMenu extends ListActivity{

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

}

这是我的错误日志:

04-21 13:22:09.389: D/skia(1365): --- decoder->decode returned false

04-21 13:22:09.389: D/AndroidRuntime(1365): Shutting down VM

04-21 13:22:09.401: W/dalvikvm(1365): threadid=1: thread exiting with uncaught exception (group=0x40a70930)

04-21 13:22:09.479: E/AndroidRuntime(1365): FATAL EXCEPTION: main

04-21 13:22:09.479: E/AndroidRuntime(1365): android.view.InflateException: Binary XML file line #17: Error inflating class com.android.internal.view.menu.ExpandedMenuView

04-21 13:22:09.479: E/AndroidRuntime(1365):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.view.menu.ListMenuPresenter.getMenuView(ListMenuPresenter.java:99)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.getListMenuView(PhoneWindow.java:3417)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:1130)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:578)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:847)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1520)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1854)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)

04-21 13:22:09.479: E/AndroidRuntime(1365):     at   android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)

3 个答案:

答案 0 :(得分:0)

根据此guide,您无需在覆盖该功能时调用super.onCreateOptionsMenu(menu)

尝试删除该行,看看它是否有效。

另外,请查看此帖子,这可能是您的问题:Android: What could cause an Options Menu inflation error?

答案 1 :(得分:0)

我创建了另一个项目,并将我的所有代码从旧代码复制/粘贴到新代码中,并且它有效。不知道错误是什么。 谢谢你的帮助。

答案 2 :(得分:0)

您使用的主题是“android:Theme.Material”吗? 尝试更改为“Theme.AppCompat”,即使您的主题位于值-v21。