开发者

Understanding why onCreateOptionsMenu is not displaying a menu

I'm reading through Android For Dummies. One example makes use of the onCreateOptionsMenu (I've added a log message):

@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;
}

I have no errors in Eclipse and when I run the application on the emulator, I can see the log message:

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?

The menu does not display and I'm not sure how to work out why not. Can anybody help me?

Here is 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>

Here is more 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_0000开发者_运维百科1001_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?

Here is the complete class:

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;
    }
}


I would remove the line super.onCreateOptionsMenu(menu); and try it again. This is also exemplified in Androids Inflating a Menu Resource section.


Call the super method after inflating it

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


This question is answered here:

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

I had the same problem whilst studying this book. The code is right, but you have to press the menu button in order for it to appear. :)


onCreateOptionsMenu() is called when activity created .We dont need to call it explicitly. Check whether your activity has title bar or not.If dont then make it visible or remove no title bar property in activity.Try it may it work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜