2016-03-01 72 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. (可選)清洗並重新構建項目,現在一切都應該工作。

我自己試了一下,它在我的平臺上工作。希望有所幫助。