如何从菜单切换到新的活动?

发布于 2024-09-16 04:34:11 字数 644 浏览 1 评论 0原文

我有一个菜单,想在用户单击菜单项时打开一个新的活动:

@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

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

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

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

发布评论

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

评论(5

流殇 2024-09-23 04:34:12

this.getContext() 更改为 this.getApplicationContext()

您正在尝试调用不存在的方法。

Change this.getContext() to this.getApplicationContext()

You are trying to call a method that doesn't exist.

不一样的天空 2024-09-23 04:34:12

在你的活动类中做

Context mContext;

在你的 onCreate() 中做

mContext = this

然后在你的选项中做

Intent myIntent = new Intent(this.getContext(), AddStocksActivity.class);

In your activity class do

Context mContext;

In your onCreate() do

mContext = this

then in your Options thing, do

Intent myIntent = new Intent(this.getContext(), AddStocksActivity.class);
天荒地未老 2024-09-23 04:34:12
 Intent myIntent = new Intent(getApplicationContext(), AddStocksActivity.class);

或者

Intent myIntent = new Intent(this, AddStocksActivity.class)
 Intent myIntent = new Intent(getApplicationContext(), AddStocksActivity.class);

or

Intent myIntent = new Intent(this, AddStocksActivity.class)
云淡风轻 2024-09-23 04:34:11

由于 Main 扩展了 Activity (它扩展了 Context),您可以执行以下操作:

Intent myIntent = new Intent(this, AddStocksActivity.class)

Since Main extends Activity (which extends Context), you can do:

Intent myIntent = new Intent(this, AddStocksActivity.class)
没有心的人 2024-09-23 04:34:11

这就是我所做的

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    MenuItem item1 = menu.findItem(R.id.menu_item_a);
    Intent intent1 = new Intent(this, A.class);
    item1.setIntent(intent1);

    MenuItem item2 = menu.findItem(R.id.menu_item_b);
    Intent intent2 = new Intent(this, B.class);
    item2.setIntent(intent2);

}

希望有帮助

this is what I do

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    MenuItem item1 = menu.findItem(R.id.menu_item_a);
    Intent intent1 = new Intent(this, A.class);
    item1.setIntent(intent1);

    MenuItem item2 = menu.findItem(R.id.menu_item_b);
    Intent intent2 = new Intent(this, B.class);
    item2.setIntent(intent2);

}

hope it helps

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