了解onCreateOptionsMenu未显示菜单的原因

时间:2011-07-10 11:04:14

标签: android

我正在阅读Android For Dummies。一个例子使用了onCreateOptionsMenu(我添加了一条日志消息):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    Log.d("ReminderListActivity","Why is there no menu?");
    return true;
}

我在Eclipse中没有错误,当我在模拟器上运行应用程序时,我可以看到日志消息:

07-10 11:38:56.993: WARN/KeyCharacterMap(382): No keyboard for id 0
07-10 11:38:56.993: WARN/KeyCharacterMap(382): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-10 11:38:57.023: DEBUG/hello(382): Why is there no menu?

菜单没有显示,我不知道如何找出原因。有人能帮助我吗?

这是list_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/menu_insert"
    android:icon="@android:drawable/ic_menu_add"
    android:title="@string/menu_insert"
    />
</menu>

以下是更多LogCat:

07-10 18:15:11.501: DEBUG/KeyguardViewMediator(58): pokeWakelock(5000)
07-10 18:15:11.881: DEBUG/KeyguardViewMediator(58): pokeWakelock(5000)
07-10 18:15:12.201: INFO/ARMAssembler(58): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x356548:0x356654] in 1684001 ns
07-10 18:15:12.251: INFO/ARMAssembler(58): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x356658:0x356820] in 901000 ns
07-10 18:15:19.494: WARN/KeyCharacterMap(296): No keyboard for id 0
07-10 18:15:19.494: WARN/KeyCharacterMap(296): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-10 18:15:19.501: DEBUG/ReminderListActivity(296): Why is there no menu?
07-10 18:15:39.422: DEBUG/AndroidRuntime(303): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
07-10 18:15:39.422: DEBUG/AndroidRuntime(303): CheckJNI is ON
07-10 18:15:39.671: DEBUG/AndroidRuntime(303): --- registering native functions ---
07-10 18:15:40.621: DEBUG/AndroidRuntime(303): Shutting down VM
07-10 18:15:40.831: DEBUG/jdwp(303): adbd disconnected
07-10 18:15:40.851: INFO/AndroidRuntime(303): NOTE: attach of thread 'Binder Thread #3' failed
07-10 18:15:41.671: DEBUG/AndroidRuntime(311): >>>>>> >>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
07-10 18:15:41.671: DEBUG/AndroidRuntime(311): CheckJNI is ON
07-10 18:15:41.891: DEBUG/AndroidRuntime(311): --- registering native functions ---
07-10 18:15:42.861: INFO/ActivityManager(58): Force stopping package com.dummies.android.taskreminder uid=10034
07-10 18:15:42.891: INFO/Process(58): Sending signal. PID: 296 SIG: 9
07-10 18:15:42.971: INFO/WindowManager(58): WIN DEATH: Window{450347c8 com.dummies.android.taskreminder/com.dummies.android.taskreminder.ReminderListActivity paused=false}
07-10 18:15:42.991: INFO/UsageStats(58): Unexpected resume of com.android.launcher while already resumed in com.dummies.android.taskreminder
07-10 18:15:43.061: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.dummies.android.taskreminder/.ReminderListActivity }
07-10 18:15:43.081: DEBUG/AndroidRuntime(311): Shutting down VM
07-10 18:15:43.091: DEBUG/dalvikvm(311): Debugger has detached; object registry had 1 entries
07-10 18:15:43.141: INFO/AndroidRuntime(311): NOTE: attach of thread 'Binder Thread #3' failed
07-10 18:15:43.371: INFO/ActivityManager(58): Start proc com.dummies.android.taskreminder for activity com.dummies.android.taskreminder/.ReminderListActivity: pid=318 uid=10034 gids={}
07-10 18:15:43.411: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 296 uid 10034
07-10 18:15:43.961: WARN/ActivityThread(318): Application com.dummies.android.taskreminder is waiting for the debugger on port 8100...
07-10 18:15:44.011: INFO/System.out(318): Sending WAIT chunk
07-10 18:15:44.311: INFO/dalvikvm(318): Debugger is active
07-10 18:15:44.441: INFO/System.out(318): Debugger has connected
07-10 18:15:44.451: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:44.651: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:44.851: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:45.051: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:45.081: INFO/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x369920:0x3699dc] in 556000 ns
07-10 18:15:45.251: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:45.461: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:45.662: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:45.861: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:46.061: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:46.271: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:46.473: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:46.675: INFO/System.out(318): waiting for debugger to settle...
07-10 18:15:46.881: INFO/System.out(318): debugger has settled (1404)
07-10 18:15:48.112: INFO/ActivityManager(58): Displayed activity com.dummies.android.taskreminder/.ReminderListActivity: 4788 ms (total 4788 ms)
07-10 18:15:53.321: DEBUG/dalvikvm(129): GC_EXPLICIT freed 2287 objects / 109048 bytes in 114ms
07-10 18:16:11.591: WARN/KeyCharacterMap(318): No keyboard for id 0
07-10 18:16:11.591: WARN/KeyCharacterMap(318): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-10 18:16:11.621: DEBUG/ReminderListActivity(318): Why is there no menu?

这是完整的课程:

package com.dummies.android.taskreminder;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ReminderListActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list);

        String[] items = new String[] {"Foo","Bar", "Fizz", "Bin"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);

        setListAdapter(adapter);

        registerForContextMenu(getListView());
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ReminderEditActivity.class);
        i.putExtra("RowId", id);
        startActivity(i);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.list_menu, menu);
        Log.d("ReminderListActivity","Why is there no menu?");
        return true;
    }
}

4 个答案:

答案 0 :(得分:1)

我会删除该行super.onCreateOptionsMenu(menu);并再次尝试。在Androids Inflating a Menu Resource部分也举例说明了这一点。

答案 1 :(得分:1)

在充气后调用super方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

答案 2 :(得分:1)

这个问题在这里得到解答:

My menu in my activity is not appearing because my onCreateOptionsMenu is not getting called

我在学习本书时遇到了同样的问题。代码是正确的,但您必须按菜单按钮才能显示。 :)

答案 3 :(得分:1)

onCreateOptionsMenu()在创建活动时被调用。我们不需要显式调用它。 检查您的活动是否有标题栏。如果没有,请在活动中将其显示或删除没有标题栏属性。尝试它可能有效。