Android Developers MyFirstApp - “菜单无法解析或不是字段”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11989214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:23:49  来源:igfitidea点击:

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

androideclipse

提问by Shocker_33

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

我正在尝试使用Eclipse 中android 提供教程构建我的第一个应用程序。

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.

我在其他问题上找到的建议我尝试点击它并包括它要求的内容是“在类型 'R' 中创建字段'菜单'或在类型'R'中创建常量'菜单' . 我已经单独完成了,每次我保存项目以便重建时,它会自动删除包含的行。

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

对于该领域,这已经完成。- 公共静态对象菜单;对于常量,这已经完成了。- public static final String menu = null;

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

所以我现在有点卡住了,希望得到一些急需的帮助。

回答by fweigl

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

onCreateOptionsMenu 方法指的是必须在 /res/menu 文件夹中并以 XML 实现的菜单。看起来像这样:

<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

您可以复制/粘贴此代码,将其另存为 /res/menu 文件夹中的 activity_main.xml(如果没有,请创建一个)或删除整个方法

@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.

这样做是在您点击设备上的菜单按钮时打开一个菜单,如果您删除该方法,则不会有这样的菜单。如果需要,您仍然可以创建一个。

回答by user389061

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

检查并查看您是否正在导入 android.R。如果是这样,请删除该行。

回答by Serdar Samanc?o?lu

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

确保将 menu.xml 文件放在 res/menu/ 目录中。

回答by Muhammad Sadiq

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.

在 Eclipse 中使用 Eclipse IDE--->Package Explore > res > menu > main.xml 或 main_activity_actions 文件...检查其名称..并放入语句 inflater.inflate(R.menu.main, menu); 您在 inflater 语句中使用的名称无法解析或者是您的应用程序中的字段..所以首先确认它然后菜单目录中的正确文件名是什么,然后在您的语句中使用它。