2016-03-01 31 views
0

以下是我的android程序的代码片段,我面临着两个错误。一个是在R.menu.menu_main,它显示主要错误为Cannot Resolve Symbol "menu",另一个在R.id.action_settings,它也显示action_settings Cannot Resolve the Symbol无法解析符号'action_settings'

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 

    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

这里是activity_main XML文件

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 
+0

在人们开始建议如何解决“无法解决问题”之前,您可以发布您的menu_main.xml吗?并告诉我们它存储在哪个文件夹? –

+0

检查您的R进口 – njzk2

回答

-1

尝试通过单击生成>清理了Android Studio中的菜单栏清洗项目。

+0

我试过清洗,但仍然有错误..... –

0

它在片段不同

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



    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

,也不要忘了在OnCreate中添加片段的菜单

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
    } 

样品:

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
      android:id="@+id/menu_check" 
      android:title="@string/done" 
      app:showAsAction="always"/> 

</menu> 
+0

仍然没有错误存在...... –

+0

@RajuIn你在做碎片吗? – IgorB

+0

@RajuIn你也确定你在这个res/menu/menu_main的路径中创建菜单吗? – IgorB

1

问题是由于事实上(可能!)你没有任何菜单文件声明与您尝试访问的属性,因此当R类是建立,它不能只看到他们。

为了解决这个问题,只是尝试以下步骤:

  1. 创建目录/app/res命名menu(右键点击 res目录,不是选择在上下文菜单中new directory和 命名menu
  2. 创建一个新的xml文件menu目录只是 创建并命名它menu_game(上menu目录中点击右键, 选择XML在上下文菜单,然后选择布局XML文件,并将其命名 menu_game
  3. 打开创建menu_game.xml和剪切/粘贴以下代码:

    <item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="my menu Item!" 
    app:showAsAction="never"/> 
    
  4. (可选)如果应用程序未被正确识别(最后一行是红色的...)尝试通过按alt-enter快捷键(光标在确切的线上)来修复它。这应该为应用程序声明一个名称空间(一个模式来检索它)。您的最终menu_game.xml文件应该是这个样子:

    <?xml version="1.0" encoding="utf-8"?> 
        <menu xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto"> 
        <item 
        android:id="@+id/action_settings" 
        android:orderInCategory="100" 
        app:showAsAction="never"/> 
    </menu> 
    
  5. (可选)清洗并重新构建项目,现在一切都应该工作。

我自己试了一下,它在我的平台上工作。希望有所帮助。