如何从Android的菜单项呼叫活动? [英] How to call Activity from a menu item in Android?

查看:132
本文介绍了如何从Android的菜单项呼叫活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用startActivity(myIntent)从菜单按钮的点击,但我的应用程序崩溃,在这一点上。

同样startActivity电话工作正常,从一个普通按钮点击,所以,我想在菜单按钮丢失有关上下文的信息?或者,也许我完全掉在这里的标志。

所以...什么是有一个菜单项带我去一个特定的活动的正确方法?

根据初始设置建议我修改后的我的code。尽管如此崩溃在同一个地方。调试器不进入例外条款,该应用程序刚刚去世。

 公共布尔onCreateOptionsMenu(功能菜单){
  MenuInflater充气= getMenuInflater();
  inflater.inflate(R.menu.menu,菜单);
  返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
   尝试{
    开关(item.getItemId()){
    案例R.id.menuItemLang:
        startActivity(新的意向书(com.my.project.SETTINGS));
        返回true;
    默认:
        返回super.onOptionsItemSelected(项目);
    }
   }赶上(例外五){
      登录(E);
   }
}
 

解决方案

首选项

您必须在您的活动,当用户单击选项菜单中的项目被称为覆盖 onOptionsItemSelected 方法。在该方法中,你可以检查哪些项目已被点击。

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例R.id.menu_item1:
        意向意图=新的意图(这一点,ActivityForItemOne.class);
        this.startActivity(意向);
        打破;
    案例R.id.menu_item2:
        //另一个startActivity,这是ID为menu_item2项目
        打破;
    默认:
        返回super.onOptionsItemSelected(项目);
    }

    返回true;
}
 

还有 onContextItemSelected 方法,它的工作原理类似地,但是对于上下文菜单(我不知道,什么菜单中,你的意思)。

在<一个更多信息href="http://developer.android.com/guide/topics/ui/menus.html">http://developer.android.com/guide/topics/ui/menus.html

编辑:

第二个选项

我觉得第一个选项是比较容易,但是从你的code我明白了,你想要的,因为字符串参数来启动活动作为动作(意图的构造函数)。要做到这一点,你需要指定你的Andr​​oidManifest.xml中的作用。所以,如果我将开始活动 ActivityForItemOne (由previous为例)&lt;应用&gt;在AndroidManifest 元素的.xml是这样的:

 &lt;应用...&GT;
    ...

    &LT;活动机器人:标签=活动第一项机器人:名称=。ActivityForItemOne&GT;
        &LT;意向滤光器&gt;
            &lt;作用机器人:名称=my.app.ITEMONE/&GT;
            &LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
        &所述; /意图滤光器&gt;
    &LT; /活性GT;
&LT; /用途&gt;
 

意图将是:

 意向意图=新的意图(my.app.ITEMONE);
 

my.app。包是您的应用程序。这是没有必要使用你的应用程序包,但建议对行动的独特性。

更多信息请访问:

类意图 - 动作和类别常量

动作元素

意图和意图过滤器

希望这会解决您的问题。

I'm attempting to call startActivity(myIntent) from the click of a menu button but my application crashes at that point.

The same startActivity call works fine from a regular button click, so, I assume the menu button is missing information about the context? Or maybe I'm totally off the mark here.

So... what's the correct way to have a menu item take me to a specific Activity?

I've revised my code based on the initial set of advice. Still crashing in the same place. The debugger doesn't enter the exception clause, the app just dies.

[EDITED WITH CODE SNIPPET]

public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.menu, menu);
  return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
   try{
    switch (item.getItemId()) {
    case R.id.menuItemLang:            
        startActivity(new Intent("com.my.project.SETTINGS"));
        return true;        
    default:
        return super.onOptionsItemSelected(item);
    }
   }catch(Exception e){
      log(e);
   }
}

解决方案

First option

You have to override onOptionsItemSelected method in your Activity, which is called when user clicks on the item in Options menu. In the method you can check what item has been clicked.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_item1:
        Intent intent = new Intent(this, ActivityForItemOne.class);
        this.startActivity(intent);
        break;
    case R.id.menu_item2:
        // another startActivity, this is for item with id "menu_item2"
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    return true;
}

There is also onContextItemSelected method which works similary, but for Context menu (I'm not sure, what menu you mean).

More information at http://developer.android.com/guide/topics/ui/menus.html

EDIT:

Second option

I think the first option is easier, but from your code I see, that you want to start activity as an action (because of String parameter in Intent constructor). To do this, you need to specify an action in your AndroidManifest.xml. So, if I would start activity ActivityForItemOne (from previous example) the <application> element in AndroidManifest.xml would look like this:

<application ...>
    ...

    <activity android:label="Activity For First Item" android:name=".ActivityForItemOne">
        <intent-filter>
            <action android:name="my.app.ITEMONE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

And the Intent will be:

Intent intent = new Intent("my.app.ITEMONE");

The my.app. is package of your application. It's not necessary to use your application package, but it's recommended for uniqueness of actions.

More information at:

Class Intent - Action and Category constants

Action element

Intents and Intent Filters

Hope this solve your problem.

这篇关于如何从Android的菜单项呼叫活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆