我有一个菜单,希望在用户单击菜单项时打开一个新活动:
@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);
}
}
我不知道如何正确地创造我的意图
编译器错误:
The method getContext() is undefined for the type Main
最佳答案
由于Main
扩展Activity
(扩展Context
),您可以:
Intent myIntent = new Intent(this, AddStocksActivity.class)