开发者

Menu Intent in android

开发者 https://www.devze.com 2023-01-18 20:48 出处:网络
I have this menu example But in this example new activity is not called, please give me the solution package Com.ContactProject;
      I have this menu example But in this example new activity is not called,
      please give me the solution

      package Com.ContactProject;





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

      import android.view.MenuItem;

     public class Contact extends Activity {
      /** Called when the activity is first created. */
       private static final int MENU_NEW_GAME = 0;
       private static final int MENU_QUIT = 1;
       private static final int MENU_COUNTRIES = 2;

       @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.main);



}
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    return true;

}

public boolean onOptionsItemSelected(MenuItem item) {

    switc开发者_JAVA技巧h (item.getItemId()) {
    case MENU_NEW_GAME:
        Intent cityMap = new Intent(Contact.this, Add.class);
        startActivity(cityMap);
        //newGame();
        return true;
    case MENU_QUIT:
        Intent cityMap1 = new Intent(Contact.this, Sms.class);
        startActivity(cityMap1);
        //quit();
        return true;
    case MENU_COUNTRIES:
        Intent cityMap2 = new Intent(Contact.this, Update.class);
        startActivity(cityMap2);
        return true;

    default:

    }
    return false;
}

}


Your Switch case must be like the following

Code borrowed from here

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.new_game:
        newGame();
        return true;
    case R.id.quit:
        quit();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Don't use your own Constants, use the ones that are generated in R file.

0

精彩评论

暂无评论...
验证码 换一张
取 消