活动在点击菜单项时不打开

huangapple go评论92阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年5月30日 14:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定