问题描述
我在获取onOptionsItemSelected()
方法之外的菜单项的引用时遇到问题.我希望在创建Activity
后立即执行该部分代码,因此我试图在onCreate()
中使用该菜单项,但它始终返回null
.任何帮助将不胜感激.
I am facing a problem in getting a reference to a menu item outside onOptionsItemSelected()
method. I want that part of code to be executed as soon as my Activity
is created, so I am trying to use that menu item in onCreate()
but it is always returning null
. Any help would be greatly appreciated.
我正在尝试以下代码:
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_task_detail);
updateMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sub_task_detail, menu);
this.menu = menu;
menuItem = menu.findItem(R.id.markComplete);
return true;
}
public void updateMenu(){
menuItem = menu.findItem(R.id.markComplete);
SharedPreferences sharedPreferences1 = getSharedPreferences("StatusValue", Context.MODE_PRIVATE);
int count2 = sharedPreferences1.getInt("status", 0);
if (count2 != 0){
menuItem.setEnabled(false);
}
else {
menuItem.setEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar 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.
SharedPreferences sharedPreferences1 = getSharedPreferences("StatusValue", Context.MODE_PRIVATE);
int count2 = sharedPreferences1.getInt("status", 0);
if (count2 != 0){
menuItem.setEnabled(false);
}
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.markComplete) {
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
String taskname = task_name.getText().toString();
int subTaskNumber1 = db.getSubTaskUID(taskname);
int taskNumber = db.getTaskUID(position1);
Log.d("Pana", "The value of Sub Task Number is " +subTaskNumber1);
Long result = db.updateUserTable(1, subTaskNumber1, taskNumber);
//Toast.makeText(this, "The value of result is " + result, Toast.LENGTH_LONG).show();
// getting current Date and Time
Calendar c = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String formattedDate = df1.format(c.getTime());
// formattedDate have current date/time
Log.d("Pana", "Formatted Date is " + formattedDate);
String[] dateTimeSplit = formattedDate.split(" ");
String currDate = dateTimeSplit[0];
String currTime = dateTimeSplit[1];
subTask = db.getSubTask(position);
String subTaskName = subTask.getSub_Task_Name();
int UID = db.getSubTaskUID(subTaskName);
db.updateEstCompTimeDateSubTask(UID, currTime, currDate);
String[] actTimeDate = db.getActTimeDateSubTask(taskname);
String actTime = actTimeDate[0];
String actDate = actTimeDate[1];
act_compDate.setText(actTime);
act_compTime.setText(actDate);
int count = db.checkOtherSubTask(taskNumber);
if (count == 0){
Toast.makeText(this, "There are no active Sub Tasks for the corresponding Task. Please create a new Sub Task or mark the corresponding task as completed", Toast.LENGTH_LONG).show();
}
int count1 = db.getStatus(taskNumber, subTaskNumber1);
SharedPreferences sharedPreferences = getSharedPreferences("StatusValue", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("status", count1);
editor.commit();
if (count1 != 0) {
item.setEnabled(false);
}
}
return super.onOptionsItemSelected(item);
}
在这里,我想在我的onCreate方法中设置ID为R.id.markComplete的项目,但是我无法获得对该项目的引用.欢迎提出所有建议.预先感谢.
Here I want to set the item with id R.id.markComplete in my onCreate method, but I am unable to get a reference to that item. All suggestions are welcome. Thanks in advance.
推荐答案
建议您不要做自己想做的事情.当您可以创建一个类字段
Its not advisable to do what you want. While you can create a class field
private Menu menu;
并在onCreateOptionsMenu()
this.menu = menu;
已知menu
的值在Activity
/Fragment
生命周期的某些点突然变为null
.因此,建议您使用onCreateOptionsMenu()
,onPrepareOptionsMenu()
和onOptionsItemSelected()
方法中的menu
做所有您想做的事情.这些方法是执行此操作的正确位置.
it is known that the value of menu
abruptly becomes null
at certain points in the Activity
/ Fragment
life-cycle. Hence you are advised to do everything you want with menu
in the onCreateOptionsMenu()
, onPrepareOptionsMenu()
and onOptionsItemSelected()
methods. Those methods are the right place to do that.
参考:
1.. 菜单 .
2.. 选项菜单教程 .
这篇关于我们如何像在onCreate()中一样使用onOptionItemSelected之外的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!