我在获取onOptionsItemSelected()
方法之外的菜单项的引用时遇到问题。我希望在创建Activity
后立即执行该部分代码,因此我尝试在onCreate()
中使用该菜单项,但它始终返回null
。任何帮助将不胜感激。
我正在尝试以下代码:
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的项目,但是我无法获得对该项目的引用。欢迎所有建议。提前谢谢。
答案 0 :(得分:2)
不建议做你想做的事。当你可以创建一个类字段
private Menu menu;
并在onCreateOptionsMenu()
this.menu = menu;
众所周知,menu
的值在null
/ Activity
生命周期的某些点突然变为Fragment
。因此,建议您使用menu
,onCreateOptionsMenu()
和onPrepareOptionsMenu()
方法中的onOptionsItemSelected()
执行所需操作。这些方法是正确的做法。
<强>参考文献:强>
1。 Menus。