无法从Android中选择的OnOptions Item完成活动
我正在尝试从菜单选项关闭“活动”。当选择menuItem无法从Android中选择的OnOptions Item完成活动,android,android-activity,Android,Android Activity,我正在尝试从菜单选项关闭“活动”。当选择menuItem菜单\u close\u activity时(调试时),我注意到调试器总是从return true步骤跳到default。 我尝试使用ActivityClassName.this.finish(),但仍然得到相同的结果 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
菜单\u close\u activity
时(调试时),我注意到调试器总是从return true步骤跳到default。
我尝试使用ActivityClassName.this.finish()
,但仍然得到相同的结果
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_xxxx:
break;
case R.id.menu_yyyy:
break;
case R.id.close_activiy:
// doing some stuff here;
setResult(0001);
finish(); // Debugger jumps from here
return true;
default:
return super.onOptionsItemSelected(item); // Debugger jumps to here.
}
}
为什么我会跳转到默认值,而不会返回真值
除了此方法之外,我还有
public boolean oncreateoptions Menu(Menu Menu)
什么都不做,只是打开选项菜单,以及protected void onCreate(Bundle savedInstanceState)
我看不出你的活动没有结束的原因。应该这样。您能否将一些日志添加到
onDestroy()
方法中,并查看它是否被调用
至于调试器
为什么会跳转,那么,在这种情况下,不要信任调试器。我认为它跳跃是因为在不同的线程中发生了不同的事情。而是添加一些日志以查看实际发生的情况。用以下代码替换代码并检查日志
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_xxxx:
break;
case R.id.menu_yyyy:
break;
case R.id.close_activiy:
// doing some stuff here;
Log.d("DEBUG","BEFORE FINISH = "+item.toString());
setResult(1);
finish(); // Compiler jumps from here
Log.d("DEBUG","AFTER FINISH = "+item.toString());
return true;
default:
Log.d("DEBUG","DEFAULT STATEMENT = "+item.toString());
return super.onOptionsItemSelected(item); // Compiler jumps to here.
}
}
编译器还是调试器?你能发布整个类吗?可能是return语句有问题。尝试中断。为什么
R.id.menu\u log\u out
不在开关盒条件下?什么是R.id.close\u活动
?这是菜单项吗?小心使用0001作为数字-它将被解释为八进制-请参阅:10-27 21:37:19.259 2066-2066/com.example.mohgeis.driveschoolapp I/DEBUG:BEFORE FINISH=close 10-27 21:37:19.273 2066-2066/com.example.mohgeis.driveschoolapp I/DEBUG:AFTER FINISH=close 10-27 21:37:19.4982066-2097/com.example.mohgeis.driveschoolapp W/EGL_仿真:eglSurfaceAttrib未实现10-27 21:37:19.499 2066-2097/com.example.mohgeis.driveschoolapp W/OpenGLRenderer:未能在表面0xad75ffa0上设置EGL_交换_行为,error=EGL_SUCCESS 10-27 21:37:19.721 2066-2097/com.example.mohgeis.driveschoolapp E/Surface:getSlotFromBufferLocked:unknown buffer:0xAB7D6130确实,当我运行应用程序并使用日志时,没有触发任何默认语句,但上面层活动中返回的resultCode为0而不是1活动正在关闭。我发现很奇怪,调试误导了我,并且把我带错了方向,非常感谢。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_xxxx:
break;
case R.id.menu_yyyy:
break;
case R.id.close_activiy:
// doing some stuff here;
Log.d("DEBUG","BEFORE FINISH = "+item.toString());
setResult(1);
finish(); // Compiler jumps from here
Log.d("DEBUG","AFTER FINISH = "+item.toString());
return true;
default:
Log.d("DEBUG","DEFAULT STATEMENT = "+item.toString());
return super.onOptionsItemSelected(item); // Compiler jumps to here.
}
}