Android Developers MyFirstApp - “menu cannot be re

2020-07-09 09:43发布

问题:

I am attempting to build my first app using the tutorial provided by android in Eclipse.

I've done everything is says but i get the error message..."menu cannot be resolved or is not a field". The tutorial doesn't mention anything about a menu and i'm sure i haven't done anything to it. The line it is complaining about i've marked in a comment.

package com.example.my.first.app;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);        <!--this line-->
    return true;
}
}

This is the main activity page the tutorial has asked me to edit is shown below...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />
</LinearLayout>

Going off advice i've found on other questions i've attempted clicking on it and to include what ever it's asking for which is "Create field 'menu' in type 'R' or "Create constant 'menu' in type 'R'. I've done both separately and every time i save the project so it rebuilds, it automatically deletes the line that was included.

For the field, this was done. - public static Object menu; For the constant, this was done. - public static final String menu = null;

So i'm now a little stuck and would like some much needed help please.

回答1:

The onCreateOptionsMenu method refers to a menu that has to be in /res/menu folder and is implemented as XML. Looks like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
    android:title="@string/menu_settings"
    android:orderInCategory="100" />
</menu>

You can copy/ paste this code, save it as activity_main.xml in your /res/menu folder (create one if you dont have one) or just delete the whole method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);        <!--this line-->
    return true;
}

What this does is open a menu when you hit the menu button on the device, if you delete the method, you wont have such a menu. You can still create one if you need one.



回答2:

Check and see if you are importing android.R. If so, remove that line.



回答3:

Make sure you put your menu.xml file in res/menu/ directory.



回答4:

Using Eclipse IDE--->Package Explore in Eclipse > res > menu > main.xml or main_activity_actions files... check its name.. and put into statement inflater.inflate(R.menu.main, menu); the name you are using in inflater statement can't be resolved or is field in your app.. so first confirm it then that what is the correct file name in menu dirctory and then use it in your statement.