0

好的,这是我的 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>

加载没有问题。按下菜单按钮......什么都没有。没有错误,什么都没有。

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

4

2 回答 2

1

您说您的 XML 菜单文件是options.xml- 所以您需要更改inflater.inflate(R.menu.option, menu);binflater.inflate(R.menu.options, menu);

于 2013-03-26T01:55:18.640 回答
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);
}
于 2013-03-26T05:47:04.480 回答