【问题标题】:inflater.inflate() ,menu cannot be resolved or is not a field [closed]inflater.inflate() ,菜单无法解析或不是字段[关闭]
【发布时间】:2013-08-19 15:18:29
【问题描述】:

我用的是inflater.inflate(R.menu.menu, menu);,但是这行有这个错误:

menu cannot be resolved or is not a field

我在这个方法中使用了上面的函数:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;

    // Inflate the currently selected menu XML resource.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return true;
}

我在/res/menu 文件夹中有menu.xml,它的内容:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:title="@string/settings"/>
    <item
        android:id="@+id/action_help"
        android:title="@string/help"/>
    <item
        android:id="@+id/action_about"
        android:title="@string/about"/>
    <item
        android:id="@+id/action_support"
        android:title="@string/support"/>

</menu> 

并且,在R.java R.menu 中定义:

public static final class menu {
        public static final int menu=0x7f080000;
        public static final int title_only=0x7f080001;
    }

我在 Stackoverflow 中看到了一些重复的问题,但任何答案都无法帮助我解决这个问题

请帮我解决这个问题

谢谢

=========================

感谢所有的朋友,一切似乎都是正确的,但还是有问题 我完全关闭 Eclipse 并重新启动笔记本电脑,重新启动后打开 Eclipse 并运行我的应用程序,它可以正常工作并且没有错误。 我真的不明白这是什么问题

【问题讨论】:

  • 确保您的导入中没有 android.R。
  • 你的第二个参数menu是什么?
  • @Egor,我检查了导入,但没有看到 android.R
  • @njzk2,我在上面的帖子中添加了我使用它的完整方法

标签: android menu inflate


【解决方案1】:

我认为您是导入的 android.R 文件,这就是为什么它会这样提供.. 检查您导入的语句并导入您的包 R.java 文件..

【讨论】:

  • 我认为 R.java 工作正常,因为在创建覆盖时,我使用 setContentView(R.layout.activity_main); 并且它没有错误
  • 我有类似的问题,问题是错误的导入。谢谢!
【解决方案2】:

您可能实例化了错误的 R.menu。您可能已经使用了 R.menu(来自 android sdk)

【讨论】:

  • 在 Eclipse 中:输入 "R" -> [CNTR]+[SPACE] -> 选择你的 "R" 实例
  • 当我添加 import android.R; 时,这一行:setContentView(R.layout.activity_main); 得到同样的错误 (menu cannot be resolved or is not a field)
  • 你不应该导入android.R你必须导入你自己的R
  • 你是这个意思吗? :import com.example.myproject.R;
  • 是的,如果“com.example.myproject.*”是你的包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2014-03-27
  • 2014-01-05
  • 2016-05-20
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多