英文:
Activity not opening when clicking menu item
问题
我正在尝试点击 id 为 "logout" 的 "登出" 选项,但没有任何反应。以下是我的代码示例:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.logout:
Intent intent = new Intent(this, Logout.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
英文:
I am trying to tap on the "logout" option whose id is logout but nothing happens. Below is my code
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
please help
答案1
得分: 0
在onCreateOptionsMenu
方法中替换:
return super.onCreateOptionsMenu(menu);
为:
return true;
并在onOptionsItemSelected()
中替换super.onOptionsItemSelected(item);
为return true
。
英文:
in onCreateOptionsMenu
method replace
return super.onCreateOptionsMenu(menu);
by
return true;
and replace super.onOptionsItemSelected(item);
in onOptionsItemSelected()
by return true
答案2
得分: 0
在onCreateOptionsMenu()
和onOptionsItemSelected()
中启动活动后,必须返回true。
正确的方法如下所示。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
英文:
You have to return true in onCreateOptionsMenu()
and in onOptionsItemSelected()
after starting the activity.
The correct way would be like this.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
答案3
得分: 0
如果你将一个覆盖的方法链接到其超类(即调用 super.someMethod()
),这意味着你正在请求超类自行处理实现。
在这里,你应该返回 true
,以告知你已成功展开了此子Activity
中的menu
,或成功处理了选项项上的onClick()
事件。
注意: 大多数情况下,你需要链接一个超类
方法调用,其中超类
负责执行一些关键的工作。例如:
public class ChildActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
//请求超类执行其关键工作
super.onCreate(savedInstanceState);
//在这里继续你的逻辑
}
}
英文:
If you chain a overridden method to its superclass(i.e call super.someMethod()
), it means you are requesting the superclass to handle the implementation on its own.
You should return true
here to tell that you successfully inflated the menu
in this child Activity
OR succesfully handled the onClick()
event on the option items.
NOTE: Mostly, you need to chain a superclass
method call, where superclass
is reponsible for doing some critical stuffs. For eg:
public class ChildActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
//request superclass to do its critical stuffs
super.onCreate(savedInstanceState);
//continue with your logics here
}
答案4
得分: -2
重启 Android Studio。如果问题仍然存在,请告诉我。
英文:
Restart Android studio. Let me know if that still persists
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论