2011-02-13 74 views
0

我正在嘗試創建菜單演示,但無法看到菜單。我只能看到佈局中的「TextView」視圖。下面是代碼:在(RES \菜單)在Android中看不到選項菜單

<?xml version="1.0" encoding="utf-8"?> 
<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
    <item 
    android:title="@string/new_game" 
    android:id="@+id/new_game"> 
    </item> 
    <item 
    android:title="@string/quit" 
    android:id="@+id/quit" 
    > 
    </item> 
    </group> 
</menu> 

demomenu.java

menu.xml文件

package com.test.demomenu; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.Toast; 


public class demomenu extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     //super.onCreateOptionsMenu(menu); 
     MenuInflater inflater=getMenuInflater(); 
     inflater.inflate(R.menu.game_menu, menu); 
     return true; 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     Toast.makeText(this, "This is the item", Toast.LENGTH_LONG).show(); 
     return true; 

    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
</LinearLayout> 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, demomenu!</string> 
    <string name="app_name">Demo Menu</string> 
    <string name="new_game">New Game</string> 
    <string name="quit">Quit</string> 
</resources> 

當我運行該項目時,我只能看到「TextView」而不是菜單。如果有人能告訴我這個應用程序出了什麼問題,我將不勝感激。

感謝, 希德

回答

1

我不認爲你可以在選項菜單中有可檢查的項目。該documentation狀態:

選項菜單:圖標菜單不支持的項目對號...

嘗試取出group標籤,看看菜單將顯示。

+0

我沒有從菜單中刪除組標籤,但仍然是同樣的問題,我該如何調試應用程序。 – Sid 2011-02-14 00:12:37

1

您正在使用R.menu.game_menu但你說你的XML命名爲menu.xml

你有什麼在logcat?你有沒有試過調試?按下菜單按鈕時調用onCreateOptionsMenu

將您的代碼與我的tutorial進行比較我無法找到重要的區別。

1

Sid ...嘗試using LogCat

所以導入

import android.util.*; 

聲明你的TAG

private static final String TAG= "Sid"; 

添加調試代碼onOptionsItemSelected

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    Log.d(TAG,"onOptionsItemSelectedCalled"); 
    return true; 

} 

運行你的應用程序,並按下菜單按鈕。現在,在過濾TAG:Sid和Log Level Debug後,查看LogCat中的消息標記:Sid消息:「onOptionsCalled」。

JAL