Android选项菜单根本不显示

时间:2013-03-26 01:38:48

标签: android android-2.3-gingerbread

好的,这是我的AndroidMenuActivity.java文件。

package com.example.caliexpeditionapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;

public class AndroidMenuActivity extends Activity {
    private WebView browser;

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

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

    public boolean onContextItemSelected(MenuItem item)
    {
        onOptionsItemSelected(item);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case R.id.menu_refresh:
                browser = (WebView) findViewById(R.id.webkit);
                browser.reload();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

和我的option.xml

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

    <item android:id="@+id/menu_refresh"
          android:icon="@drawable/ic_menu_refresh"
          android:title="@string/refresh" />

</menu>

最后是我的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.caliexpeditionapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <activity android:name="com.example.caliexpeditionapp.MainActivity"
                  android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.caliexpeditionapp.AndroidMenuActivity">
        </activity>
    </application>

</manifest>

加载没问题。按菜单按钮......没有。没有错误,只是没有。

我知道这很可能是一个重复的问题,但我不知道还有什么可以搜索。

2 个答案:

答案 0 :(得分:1)

你说你的XML菜单文件是options.xml - 因此,您需要将inflater.inflate(R.menu.option, menu);更改为binflater.inflate(R.menu.options, menu);

答案 1 :(得分:0)

添加onMenuItemSelected()

示例

/**
 * Hook called whenever an item in the options menu is selected.
 * @param item The menu item that was selected.
 * @return false to allow normal menu processing to proceed, true to consume it here.
 */
@Override
public boolean onMenuItemSelected (int featureId, MenuItem item) {
    final int itemId = item.getItemId();
    Toast.makeText(this, "Menu item clicked, index: " + itemId, Toast.LENGTH_SHORT).show();
    return super.onMenuItemSelected(featureId, item);
}