Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 316
Shocker_33 Avatar asked Aug 16 '12 14:08

Shocker_33


3 Answers

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.

like image 195
fweigl Avatar answered Sep 28 '22 15:09

fweigl


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

like image 34
user389061 Avatar answered Sep 28 '22 14:09

user389061


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

like image 40
Serdar Samancıoğlu Avatar answered Sep 28 '22 15:09

Serdar Samancıoğlu