Android Developers MyFirstApp - "the menu cannot be resolved or is not a field"

I am trying to create my first application using the tutorial provided by android in Eclipse.

I did everything that is said, but I get an error message ... "the menu cannot be resolved or is not a field." The textbook says nothing about the menu, and I'm sure I did nothing with it. In the line he complains about, I am marked in the 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 that the teacher asked to change, 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> 

Sending tips that I found on other issues, I tried to click on it and turn on what I had ever asked, for which it is “Create a menu field” in type “R” or “Create permanent menu” in type 'P' I did this both separately and every time I save the project so that it is rebuilt, it automatically deletes the line that was included.

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

So now I am a little stuck and would like to receive the necessary help.

+8
android eclipse
source share
4 answers

The onCreateOptionsMenu method refers to the menu, which should be in the / res / menu folder and implemented as XML. Looks like that:

 <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 the / res / menu folder (create it if you don’t 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 is it, the menu opens, when you press the menu button on the device, if you delete this method, you will not have such a menu. You can create it if you need it.

+9
source share

Check and make sure you import android.R. If so, delete this line.

+4
source share

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

+2
source share

Using the Eclipse IDE ---> Examine the package in Eclipse> res> menu> main.xml or main_activity_actions files ... check its name .. and enter inflater.inflate (R.menu.main, menu) in the instruction; the name you use in the inflater statement cannot be resolved or the field in your application. Therefore, first confirm this, then specify the correct file name in the dirctory menu, and then use it in your application.

0
source share

All Articles