简体   繁体   中英

Android onCreateOptionMenu not being called

I am implementing an Android Activity from which other Activities will be derived from. So basically I have this setup of an InventoryActivity and its parent class, ListActivity:

public class MyListActivity extends Activity {  
    protected Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getBaseContext();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options, menu);
        Log.d("Creating options menu", "True");
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.d("Preparing options menu", "True");
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save:
            return(true);

            case R.id.revert:
            return(true);
        }

        return(super.onOptionsItemSelected(item));
    }
}


public class InventoryActivity extends MyListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inventory);
    }
}

And I also have this in options.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/card_list_save"
          android:icon="@drawable/ic_menu_save"
          android:title="Save"/>
    <item android:id="@+id/card_list_revert"
          android:icon="@drawable/ic_menu_revert"
          android:title="Revert" />
</menu>

If necessary, this is my layout for inventory.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/callSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Search"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/inventory"/>
    </ScrollView>
</LinearLayout>

However, when I press the menu button, nothing happens. The Log messages in the onCreateOptionsMenu method does not appear. Instead all I can see is the following:

02-04 11:36:58.313: W/KeyCharacterMap(31464): No keyboard for id 0
02-04 11:36:58.313: W/KeyCharacterMap(31464): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

But what baffles me the most is that this code works in other Activities, such as my launcher Activity. But by the concept of object oriented programming, the InventoryActivity should call the overriding methods in the MyListActivity. I am completely stuck and I need help.

ListActivity is already a class in the Android SDK. My guess is that you're importing android.app.ListActivity , and not your package.

Hmm...don't know why but removing the onCreate method in MyListActivity fixed the problem. So the class now looks like this:

public class MyListActivity extends Activity {  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options, menu);
        Log.d("Creating options menu", "True");
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.d("Preparing options menu", "True");
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save:
            return(true);

            case R.id.revert:
            return(true);
        }

        return(super.onOptionsItemSelected(item));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM