android - "Expected resource of type menu"展开 MenuButton 时

标签 android android-fragments intellij-idea

所以我的布局目录中有这个名为“actionbar_buttons.xml”的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="match_parent"
      android:layout_width="match_parent">
     <item android:id="@+id/action_settings"
           android:title="Settings">
     </item>
     <item android:id="@+id/action_settings2"
           android:title="fooo">
     </item>
 </menu>

在我的 Fragment 类中,我这样调用 inflate 方法:

@Override
public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu( menu, inflater );
    inflater.inflate(R.layout.actionbar_buttons, menu);
}

现在 Intellij 提示并告诉我:

Expected resource of type menu less... (Ctrl+F1) 
Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling      Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.

代码确实有效。我只是对 Intellij 中的那个警告感到恼火,我不确定我是否做错了什么。

最佳答案

只需将xml文件移动到

"menu"

资源目录而不是

"layout"

目录。然后换行

inflater.inflate(R.layout.actionbar_buttons, menu);

 inflater.inflate(R.menu.actionbar_buttons, menu);

关于android - "Expected resource of type menu"展开 MenuButton 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392403/

相关文章:

android - 加载 Android 布局 XML 文件时出现异常

android - invalidateOptionsMenu 在 fragment 中不起作用

Android ViewPager 在 TabLayout 之上显示 View

Android Studio 2.3.2 不打开 Android *.xml 布局文件

java - 使用 IntelliJ,我如何确定特定函数是来自 Java 还是 Scala

Android删除tabwidget中标签之间的空间

android - 如何使用 XML 样式在 Android 中创建自定义按钮

c# - 我正在尝试使单声道 android 简单 "hello world"但我在模拟器中看不到 "hello world"为什么?

android - fragment 最佳实践 - Android

scala - 使用 Scala + sbt,您如何同时允许 IntelliJ 构建和命令行构建?