Android 选项菜单中的startActivity导致应用程序崩溃

发布于 2024-12-13 13:26:31 字数 696 浏览 3 评论 0原文

“我通过将活动的声明添加到清单中解决了这个问题。”

我想在按下选项菜单按钮时启动一个活动。 当我按下按钮时,应用程序崩溃。 当我调用“openAbout()”时应用程序崩溃。感谢大家的帮助。

这是我的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0,0,1,R.string.menuAbout);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    default:
        Intent i = new Intent(this, About.class);
        this.startActivity(i);
        return true;
    }
}

public void openAbout(){
       Intent i = new Intent();
       i.setClass(this, About.class);
       startActivity(i);
}

" I solved the problem by adding the declaration of the activity into Manifest. "

I want to start an activity when the optionmenu button is pressed.
When i press the button the application crash.
The application crash when i call "openAbout()" . Thanks to everyone for helping.

Here is my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0,0,1,R.string.menuAbout);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    default:
        Intent i = new Intent(this, About.class);
        this.startActivity(i);
        return true;
    }
}

public void openAbout(){
       Intent i = new Intent();
       i.setClass(this, About.class);
       startActivity(i);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-12-20 13:26:31

在开始之前,每次有问题时,请添加堆栈跟踪,以便其他人可以了解问题的类型,

首先,检查您的活动是否已经声明在您的清单文件上,

第二,尝试将您的意图的实例更改为如下所示:

Intent i = new Intent(this, About.class);
this.startActivity(i);

Before Starting , plz every time when you have a question , plz add the stack trace so others can understand the type of the problem ,

First , check if your activity is already declared on your manifest file ,

Second , try to change the instanciation of your intent to be like this :

Intent i = new Intent(this, About.class);
this.startActivity(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文