菜单按钮不起作用?

时间:2011-12-23 06:25:30

标签: android android-activity menu android-intent

我不确定我做错了什么。我正在制作一个6按钮菜单。按钮显示但不调用活动,我看不到菜单按钮上显示的任何文本。它们显示为空白,请帮助!!

package com.cerealBarApps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class FirstLoginActivity extends Activity {

    protected void onCreate(Bundle Ebenezersbundle)

    {
        super.onCreate(Ebenezersbundle);
        setContentView(R.layout.testlayout);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater awesome = getMenuInflater();
        awesome.inflate(R.menu.main_menu, menu);
        return true;
    }

    /*
     * Intent nextScreen = new Intent(getApplicationContext(),
     * AllFaculty.class); // Sending data to another Activity
     * startActivity(nextScreen);
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            Intent intent1 = new Intent(this, SMS.class);
            startActivity(intent1);
            break;
        case 2:
            Intent intent2 = new Intent(this, MenuRecieved.class);
            startActivity(intent2);
            break;
        case 3:
            Intent intent3 = new Intent(this, MenuSent.class);
            startActivity(intent3);
        case 4:
            Intent intent4 = new Intent(this, MenuSettings.class);
            startActivity(intent4);
        case 5:
            Intent intent5 = new Intent(this, MenuExit.class);
            startActivity(intent5);

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

菜单XML:

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

    <item
        android:id="@+id/menuMenu"
        android:alphabeticShortcut="m"
        android:title="MenuTest"/>
    <item
        android:id="@+id/menuNewMessage"
        android:alphabeticShortcut="n"
        android:title="New Message"/>

    <item
        android:id="@+id/menuSent"
        android:alphabeticShortcut="s"
        android:title="Sent"/>

    <item
        android:id="@+id/menuRecieved"
        android:alphabeticShortcut="r"
        android:title="Recieved"/>

    <item
        android:id="@+id/menuSettings"
        android:alphabeticShortcut="s"
        android:title="Settings"/>

    <item
        android:id="@+id/menuExit"
        android:alphabeticShortcut="e"
        android:title="Exit"/>

</menu

代码:

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuNewMessage:
            startActivity(new Intent(getApplicationContext(), SMS.class));
            return true;
        case R.id.menuSent:
            startActivity(new Intent("com.cerealBarApps"));
            return true;
        case R.id.menuRecieved:
            startActivity(new Intent("com.cerealBarApps"));
            return true;
        case R.id.menuSettings:
            startActivity(new Intent("com.cerealBarApps"));
            return true;
        case R.id.menuExit:
            startActivity(new Intent("com.cerealBarApps"));
            return true;
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

删除return false并将其替换为:

return super.onOptionsItemSelected(menuItem);  

按照以下代码

@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
    switch (menuItem.getItemId()) 
    {
    case 0:
        this.SaveData();
        break;          
        case 1:
        Intent intent2 = new Intent(DryWall.this,Help.class);
        startActivity(intent2);
        break;  
    default:
        break;
    }       
    return super.onOptionsItemSelected(menuItem);
}

答案 1 :(得分:0)

嘿伙计们我想通了,这是因为我没有做onCreateOptionsMenu方法。谢谢大家的帮助

public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, menuNewMessage, 0, "New Message");
            menu.add(0, menuSent, 0, "Outbox");
            menu.add(0, menuRecieved, 0, "Inbox");
            menu.add(0, menuSettings, 0, "Settings");
            menu.add(0, menuExit, 0, "Exit");
            return super.onCreateOptionsMenu(menu);
        }

        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case menuNewMessage:
                startActivity(new Intent(getApplicationContext(), SMS.class));
                break;
            case menuSent:
                startActivity(new Intent(getApplicationContext(), MenuSent.class));
                break;
            case menuRecieved:
                startActivity(new Intent(getApplicationContext(),
                        MenuRecieved.class));
                break;
            case menuSettings:
                startActivity(new Intent(getApplicationContext(),
                        MenuSettings.class));
                break;
            case menuExit:
                startActivity(new Intent(getApplicationContext(), MenuExit.class));
                break;
            }
            return super.onOptionsItemSelected(item);
        }
    }