Java 已调用onCreateOptionMenu(),但不起作用
我对onCreateOptionMenu()方法有问题。它不起作用Java 已调用onCreateOptionMenu(),但不起作用,java,android,xml,oncreateoptionsmenu,Java,Android,Xml,Oncreateoptionsmenu,我对onCreateOptionMenu()方法有问题。它不起作用 当我的活动有焦点时,我想显示按钮。但是什么也没发生 我真的不明白 个人活动: public class PersonnalAccountActivity extends FragmentActivity { private static final String TAG = "com.example.smartoo.Establishment.EstablishmentActivity"; private b
当我的活动有焦点时,我想显示按钮。但是什么也没发生 我真的不明白 个人活动:
public class PersonnalAccountActivity extends FragmentActivity {
private static final String TAG = "com.example.smartoo.Establishment.EstablishmentActivity";
private boolean wantGPS = false;
/** Drawer attr **/
private String[] mDrawerItemTitles;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private int positionDrawer;
public static int idUser = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences(LogInActivity.PREFS_NAME, MODE_PRIVATE);
idUser = settings.getInt("idUser", -1);
setContentView(R.layout.personnal_account_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
if (actionBar == null) {
Log.d(TAG, "actionBar is null !");
}
//Set up the left icon to go back to Home Page
actionBar.setDisplayHomeAsUpEnabled(true);
setUpDrawer();
Intent i = getIntent();
Bundle bundle = i.getExtras();
//If intent comes from home Activity
if(bundle.get("GPS") != null) {
wantGPS = bundle.getBoolean("GPS", false);
Log.d(TAG, "wantGPS 2 "+wantGPS);
}
//If intent comes from drawer button
if (bundle.get("DRAWER_ITEM") != null) {
positionDrawer = bundle.getInt("DRAWER_ITEM", 0);
mDrawerList.setItemChecked(positionDrawer, true);
setTitle(mDrawerItemTitles[positionDrawer]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
Log.d(TAG, "oncreateoptionsmenu personnal account actiity");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.actionbar, menu);
menu.findItem(R.id.menu_update).setVisible(true);
menu.findItem(R.id.menu_update).setEnabled(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar etablishment_item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
Intent result = new Intent(PersonnalAccountActivity.this, MapActivity.class);
result.putExtra("GPS", wantGPS);
setResult(RESULT_OK, result);
finish();
return true;
case R.id.menu_update:
PersonnalAccountFragment.setFieldstoEditText();
//getActionBar().setCustomView(R.layout.actionbar_layout);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/************************************************/
/***************** DRAWER ***********************/
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void setUpDrawer() {
//Get Drawer item titles
mDrawerItemTitles = getResources().getStringArray(R.array.drawer_array);
//Get DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Get Listview into drawer
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerItemTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener(this));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(getTitle());
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(getResources().getString(R.string.drawer_open));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
公共类PersonnalAccountActivity扩展了FragmentActivity{
private static final String TAG=“com.example.smartoo.restitution.restitutionactivity”;
私有布尔值wantGPS=false;
/**抽屉属性**/
私有字符串[]mDrawerItemTitles;
私有列表视图mDrawerList;
私人抽屉布局mDrawerLayout;
私有操作bardrawertoggle mDrawerToggle;
私人抽屉;
公共静态int idUser=-1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedReferences设置=GetSharedReferences(LogInActivity.PREFS\u NAME,MODE\u PRIVATE);
idUser=settings.getInt(“idUser”,-1);
setContentView(R.layout.personnal\u account\u main);
//设置操作栏。
最终ActionBar ActionBar=getActionBar();
if(actionBar==null){
Log.d(标记“actionBar为null!”);
}
//设置左侧图标以返回主页
actionBar.setDisplayHomeAsUpEnabled(true);
setUpDrawer();
Intent i=getIntent();
Bundle=i.getExtras();
//如果意图来自家庭活动
如果(bundle.get(“GPS”)!=null){
wantGPS=bundle.getBoolean(“GPS”,false);
日志d(标签“wantGPS 2”+wantGPS);
}
//如果意图来自抽屉按钮
if(bundle.get(“抽屉商品”)!=null){
positionDrawer=bundle.getInt(“抽屉项”,0);
mDrawerList.setItemChecked(positionDrawer,true);
setTitle(mDrawerItemTitles[positionDrawer]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
menu.clear();
Log.d(标记“onCreateOptions菜单个人帐户活动”);
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.actionbar,菜单);
menu.findItem(R.id.menu\u update).setVisible(true);
menu.findItem(R.id.menu\u update).setEnabled(true);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//Handle action bar etablishment_项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
开关(item.getItemId()){
案例android.R.id.home:
意图结果=新意图(PersonnalAccountActivity.this、MapActivity.class);
结果:putExtra(“GPS”,wantGPS);
setResult(结果正常,结果);
完成();
返回true;
案例R.id.菜单\更新:
PersonnalAccountFragment.setFieldstoEditText();
//getActionBar().setCustomView(R.layout.actionbar\u布局);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
/************************************************/
/*****************抽屉***********************/
/**
*使用ActionBarDrawerToggle时,必须在
*onPostCreate()和onConfigurationChanged()。。。
*/
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//将任何配置更改传递给抽屉toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
私有void setUpDrawer(){
//获取抽屉项目标题
mDrawerItemTitles=getResources().getStringArray(R.array.drawer\u数组);
//把抽屉摆好
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
//将Listview放入抽屉
mDrawerList=(ListView)findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
//使用项目设置抽屉的列表视图,然后单击listener
mDrawerList.setAdapter(新阵列适配器,
R.layout.drawer_list_item,mDrawerItemTitles);
setOnItemClickListener(新的DrawerItemClickListener(this));
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getActionBar().setDisplayHomeAsUpEnabled(true);
//ActionBarDrawerToggle将适当的交互联系在一起
//在滑动抽屉和操作栏应用程序图标之间
mDrawerToggle=新操作bardrawertoggle(
此,/*主机活动*/
mDrawerLayout,/*抽屉布局对象*/
R.drawable.ic_抽屉,/*导航抽屉图像替换“Up”插入符号*/
R.string.drawer\u open,/*“open drawer”描述,用于辅助功能*/
R.string.drawer\u close/*“close drawer”可访问性说明*/
) {
公共无效onDrawerClosed(视图){
getActionBar().setTitle(getTitle());
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
打开图纸上的公共空白(视图抽屉视图){
getActionBar().setTitle(getResources().getString(R.string.drawer_open));
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
android:title="Search"
android:visible="false"
android:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/menu_addition"
android:icon="@drawable/addition_icon"
app:showAsAction="ifRoom"
android:title="Order"
android:visible="false"/>
<item
android:id="@+id/menu_validate"
app:showAsAction="ifRoom"
android:title="Terminer"
android:visible="false"/>
<item
android:id="@+id/menu_update"
app:showAsAction="ifRoom"
android:title="Modifier"
android:visible="false"/>
</menu>