如何从菜单中切换到新的活动? [英] How to switch to new Activity from menu?
本文介绍了如何从菜单中切换到新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个菜单,想打开一个新的活动,当用户点击菜单项:
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理项目选择
开关(item.getItemId()){
案例R.id.add_symbol:
的System.out.println(添加符号CLICKED!);
意图myIntent =新的意图(this.getContext(),AddStocksActivity.class);
startActivityForResult(myIntent,0);
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
我不知道如何正确地创建我的意图
编译器错误:
的方法的getContext()是不确定的主要类型
解决方案
由于主
扩展活动
(延伸上下文
),你可以这样做:
意图myIntent =新的意图(这一点,AddStocksActivity.class)
I have a menu and would like to open a new Activity when the user clicks on the menu item:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add_symbol:
System.out.println("ADD SYMBOL CLICKED!");
Intent myIntent = new Intent(this.getContext(), AddStocksActivity.class);
startActivityForResult(myIntent, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm not sure how to create my Intent properly
Compiler Error:
The method getContext() is undefined for the type Main
解决方案
Since Main
extends Activity
(which extends Context
), you can do:
Intent myIntent = new Intent(this, AddStocksActivity.class)
这篇关于如何从菜单中切换到新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文