2 slaks SLaks 于 2013.04.18 14:34 提问

菜单按钮不能运行的问题

菜单上有6个按钮,按钮可以显示但是不能调用activity,在菜单按钮上看不到显示任何文本,出现空白,请大家帮我看看出现什么问题了。

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);
    }
}

Menu 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

code:

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;
    }
}

1个回答

guoleimail
guoleimail   2013.04.18 18:48

在所有的INTENT 前面添加一句
intent.setFlags(FLAG_ACTIVITY_NEW_TASK)
试试

Csdn user default icon
上传中...
上传图片
插入图片