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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 如何让android的屏幕保持常亮
  2. Android创建XML文档
  3. Android(安卓)studio使用JNI实例(2)- 调用
  4. AAPT: error: attribute android:request
  5. Android培训班(11)
  6. android 开发中常用到的一些代码片段(一)
  7. Cordova 3.0 + Eclipse 开发流程
  8. Android集成微信登录的步骤详解、遇到的
  9. 几行让android进入休眠的c代码
  10. Android(安卓)上传图片到 Asp.Net 服务器