Android 为什么当我点击“我的”时什么都没有发生;发送电子邮件;菜单项?

Android 为什么当我点击“我的”时什么都没有发生;发送电子邮件;菜单项?,android,email,android-intent,menu,menuitem,Android,Email,Android Intent,Menu,Menuitem,我正在写一本食谱,我遇到了一个问题-当我点击我的菜单项,它应该发送电子邮件,电子邮件地址和主题预先填写,什么都没有发生 知道为什么吗 public class recipedisplayscreen extends Activity { TextView EmailAddress; TextView EmailSubject; @Override public void onCreate(Bundle savedInstanceState) { super

我正在写一本食谱,我遇到了一个问题-当我点击我的菜单项,它应该发送电子邮件,电子邮件地址和主题预先填写,什么都没有发生

知道为什么吗

public class recipedisplayscreen extends Activity {

TextView EmailAddress;

TextView EmailSubject;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipedisplayscreen);

        TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
        TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);


        Intent i = getIntent();
        String Ingredients = i.getStringExtra("textView1");
        String Method = i.getStringExtra("textView2");
        Log.e("recipedisplayscreen", Ingredients + "." + Method);

        MethodDisplay.setText(Method);
        IngredientsDisplay.setText(Ingredients);

        EmailAddress=(TextView) findViewById(R.id.textView2); 
        EmailSubject=(TextView) findViewById(R.id.textView4); 


        ActionBar actionBar = getActionBar();
        setTitle(R.string.title);
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle(Method);}

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    // App icon in action bar clicked; go home
                    Intent intent = new Intent(this, MainScreen.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

        public boolean onOptionsItemSelected1(MenuItem recipe_suggest) {
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
            emailIntent.setType("plain/text"); 
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()}); 
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText()); 

            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            return true;
        }




     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.recipe_menu1, menu);
            return true;         
    }    
}

我相信这就是您想要的:D删除选项ItemSelected 1并用它替换选项ItemSelected,这是假设recipe\u suggest是菜单项的id

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // App icon in action bar clicked; go home
                Intent intent = new Intent(this, MainScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            case R.id.recipe_suggest:
                //Other menu item I believe
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
                emailIntent.setType("plain/text"); 
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{                 EmailAddress.getText().toString()}); 
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText()); 

                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

switch所做的是将“switch”中的对象与case匹配,匹配“switch”的case将被执行,因此如果菜单项id(MenuItem item;item.getItemId())与android.R.id.home或R.id.recipe\u建议的id匹配(我假设您在引用android R文件时使用了操作栏?)就我所知,应该这样做:)

我相信这就是你想要的:D删除选项项Selected 1并用它替换选项项Selected,这是假设recipe\u suggest是菜单项的id

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // App icon in action bar clicked; go home
                Intent intent = new Intent(this, MainScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            case R.id.recipe_suggest:
                //Other menu item I believe
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
                emailIntent.setType("plain/text"); 
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{                 EmailAddress.getText().toString()}); 
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText()); 

                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

switch所做的是将“switch”中的对象与case匹配,匹配“switch”的case将被执行,因此如果菜单项id(MenuItem item;item.getItemId())与android.R.id.home或R.id.recipe\u建议的id匹配(我假设您在引用android R文件时使用了操作栏?)就我所知,应该这样做de:)

因为您选择了两个选项?好的,其中一个名为OnOptions ItemSelected*1*,需要将其合并到真实的OnOptions ItemSelected*1*。

因为您有两个OnOptions ItemSelected?好的,一个名为onOptionsItemSelected*1*,需要将其合并到真实的一个中。

您不能只创建一个名为
onOptionsItemSelected1()
的方法,并期望android在您点击菜单项发送邮件时调用它


将此方法合并到原始的
onOptionsItemSelected()
并将其置于正确的大小写中,这样它就可以正常工作。

您不能只创建一个名为
onOptionsItemSelected1()
的方法,并期望android在您点击菜单项发送邮件时调用它


将此方法合并到原始的
onoptionItemSelected()
并将其置于正确的大小写中,这样它就可以正常工作。

为什么有一个名为onoptionItemSelected*1*的单独方法

对操作项作出反应的所有代码都应位于选项项Selected中:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // App icon in action bar clicked; go home
            Intent intent = new Intent(this, MainScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;

        case android.R.id.email_action_item:

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
            (...)
        default:
            return super.onOptionsItemSelected(item);
    }
}

为什么有一个名为OnOptions ItemSelected*1*的单独方法

对操作项作出反应的所有代码都应位于选项项Selected中:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // App icon in action bar clicked; go home
            Intent intent = new Intent(this, MainScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;

        case android.R.id.email_action_item:

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
            (...)
        default:
            return super.onOptionsItemSelected(item);
    }
}

问题是MenuItem和Menu item recipe\u suggest是两个不同的菜单?一个项目是OptionsItemSelected方法接收的内容,项目的对象类型是MenuItem,项目包含有关按下内容的基本信息,无论是主页按钮还是菜单中的任何项目,理论上这个方法仍然应该由框架调用。recipe\u suggest是一个项目,它是您需要引用的“ID”,recipe\u suggest是XML文件的一部分,对吗?这是配方菜单中的一项,对吗?你建议的ID正确吗?构建项目时,ID“存储”在R文件中,并在子类ID(在类R中)中附加了一个名为recipe\u suggest的“reference”int,该子类ID包含在类R中,类R是您引用的,然后您引用包含recipe\u suggest值的类ID,它保存的int与连接到呼叫的菜单项的int匹配:D抱歉没有问题:D安卓是一种激情,我努力帮助人们并教他们,如果我能把我的知识传授给别人,他们就可以把它传给别人,而别人也会把它传给别人。将知识传授给两个人,他们每个人都可以将知识传授给另外两个人,形成一个庞大的知识体系:D问题是菜单项和菜单项配方是两个不同的菜单?一个项目是OptionsItemSelected方法接收的内容,项目的对象类型是菜单项,项目包含有关按下内容的基本信息,无论是主页按钮还是菜单中的任何项目,理论上这个方法仍然应该由框架调用。recipe\u suggest是一个项目,它是您需要引用的“ID”,recipe\u suggest是XML文件的一部分,对吗?这是配方菜单中的一项,对吗?你建议的ID正确吗?构建项目时,ID“存储”在R文件中,并在子类ID(在类R中)中附加了一个名为recipe\u suggest的“reference”int,该子类ID包含在类R中,类R是您引用的,然后您引用包含recipe\u suggest值的类ID,它保存的int与连接到呼叫的菜单项的int匹配:D抱歉没有问题:D安卓是一种激情,我努力帮助人们并教他们,如果我能把我的知识传授给别人,他们就可以把它传给别人,而别人也会把它传给别人。把知识传授给两个人,他们每个人都可以把知识传授给另外两个人,形成一个庞大的知识学校:D