选项菜单未显示在空活动中

时间:2018-01-28 21:30:38

标签: java android menu optionmenu

我正在努力让我的选项菜单出现,并尝试了几乎所有可用于stackoverflow的解决方案。我已将我的menu.xml文件写入res的菜单目录(如下所示),并尝试将代码放在GameActivity.java文件中。当我测试我的程序时,菜单没有出现,当我点击缺少的菜单区域时没有任何反应。:

enter image description here

以下是我的活动的java代码:

public class GameActivity extends AppCompatActivity {

    int[] boardSize = {7, 5, 3, 1};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            //TODO
        }
        return true;
    }
}

这是我的菜单xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="io.github.ardenchew.lastpiece.MainActivity" >

    <item
        android:id="@+id/restart"
        android:title="@string/restart"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        tools:icon="@drawable/mr_button_light" />
    <item
        android:id="@+id/resetScore"
        android:title="@string/resetScore"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        tools:icon="@drawable/mr_button_light" />
    <item
        android:id="@+id/endGame"
        android:title="@string/endGame"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        tools:icon="@drawable/mr_button_light" />
    <item
        android:id="@+id/killCpu"
        android:title="@string/killCpu"
        app:showAsAction="always"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        tools:icon="@drawable/mr_button_light" />


</menu>

这是我的android清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.github.ardenchew.lastpiece">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PlayActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".InstructionsActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".GameActivity"
            android:screenOrientation="portrait" />
    </application>

</manifest>

非常感谢

1 个答案:

答案 0 :(得分:0)

你应该附上调试器并深入研究它。 Activity里面的代码看起来不错,也许你应该在onCreateOptionsMenu(菜单菜单)方法中调用super方法,或者问题可能在菜单xml文件中tools:context="io.github.ardenchew.lastpiece.MainActivity"