Android Tutorial(3)Android Menu Example - Utilizing Menus

Android Tutorial(3)Android Menu Example - Utilizing Menus

Trouble Shooting First
Error Message:
[INFO] :13: error: No resource identifier found for attribute 'textIsSelectable' in package 'android'
[ERROR] Error when generating sources.
org.apache.maven.plugin.MojoExecutionException:
at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:446)
at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.execute(GenerateSourcesMojo.java:162)

Solution:
That is because of the API Level. So try to get rid of this from XML configurations.

Error Message:
GcmBaseIntentService registration error account_missing

Solution:
Directly use Setting to login on with a google account.

I plan to note how to set the menus.

1. Create the XML file
Create the menu file from [res]------>[menu]----->[New Android XML File] ----->
[Resource Type] ----> Menu
Root Element -----> menu

Just click [Add Item] to add menus.

I set these properties ---> id, Title,  Icon[@drawable/menu_name] menu_name is the name of the icon file in drawable-hdpi.

2. Modify the Implementation Activity Class
public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.options_menu_all, menu);
     returntrue;
}


publicboolean onOptionsItemSelected(MenuItem item) {
     boolean result = true;
     try {
          switch (item.getItemId()) {
               caseR.id.item_list_all_person:
                    startActivity(new Intent(this, PersonListActivity.class));
                    return true;
               caseR.id.item_get_one_person:
                    startActivity(new Intent(this, GetOnePersonActivity.class));
                    return true;
               caseR.id.item_gcm_demo:
                    startActivity(new Intent(this, DemoActivity.class));
                    return true;
               caseR.id.item_list_product:
                    startActivity(new Intent(this, ProductsListActivity.class));
                    return true;
               default:
                    returnsuper.onOptionsItemSelected(item);
          }
     } catch (Exception error) {
          Log.d(TAG, "About_onOptionsItemSelected failed");
     }
     return result;
}

Some Tips:
1. If you want to show the selected item in menu. We can ask the different activity load different menu XML files with different icons.

2. We can also put the implementation of the menu in abstract class of the activities which extended from base class. That is to say, just put the menu in the base class is fine.


References:
http://vimaltuts.com/android-tutorial-for-beginners/android-menu-example

你可能感兴趣的:(android)