Android 选项菜单中的startActivity导致应用程序崩溃
“我通过将活动的声明添加到清单中解决了这个问题。”
我想在按下选项菜单按钮时启动一个活动。 当我按下按钮时,应用程序崩溃。 当我调用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在开始之前,每次有问题时,请添加堆栈跟踪,以便其他人可以了解问题的类型,
首先,检查您的活动是否已经声明在您的清单文件上,
第二,尝试将您的意图的实例更改为如下所示:
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 :