【问题标题】:Why is nothing happening when I click my "Send Email" menu item?为什么当我单击“发送电子邮件”菜单项时没有任何反应?
【发布时间】:2012-05-04 11:54:08
【问题描述】:

我正在写一本食谱书,但遇到了一个问题 - 当我点击我的菜单项时,该菜单项应该发送电子邮件,并预先填写了电子邮件地址和主题,但没有任何反应...

任何想法为什么???

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;         
    }    
}

【问题讨论】:

    标签: android email android-intent menu menuitem


    【解决方案1】:

    我相信这就是你想要的:D 删除 onOptionsItemSelected1 并用这个替换 onOptionsItemSelected,这是假设 recipe_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()) 匹配ID android.R.id.home 或 R.id.recipe_suggest (我假设您使用的是操作栏,因为您指的是 Android R 文件?)据我所知,这应该是如何做到的 :)

    【讨论】:

    • 问题是 MenuItem item 和 Menu Item recipe_suggest 是两个不同的菜单?
    • 一个item是onOptionsItemSelected方法接收到的,item的对象类型是MenuItem,item保存了被按下的基本信息,不管是home键还是其他菜单中的项 理论上该方法仍应由框架调用。
    • recipe_suggest 是一个项目,它是您需要参考的“ID”,recipe_suggest 是 XML 文件的一部分,对吗?它是 recipe_menu1 中的一个项目,对吗? ID recipe_suggest 正确吗?当您构建项目时,ID 被“存储”在 R 文件中,并且附加了一个“引用”int,其名称为 recipe_suggest 在类 R 内的子类 id 内,类 R 是您引用的然后,您引用保存值 recipe_suggest 的类 id,它保存的 int 与附加到调用的菜单项的 int 匹配。 :D 对不起
    • 没问题 :D Android 是一种激情,我努力帮助人们并教他们,如果我可以将我的知识传授给某人,那么他们可以将其传授给其他人,然后再传给其他人。将知识传授给两个人,他们每个人都可以将知识传授给另外两个人,从而创建一个庞大的知识学院:D
    【解决方案2】:

    因为你有两个onOptionsItemSelected?嗯,一个叫做 onOptionsItemSelected*1* ,需要把它合并到真正的那个。

    【讨论】:

      【解决方案3】:

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

      将此方法合并到原始onOptionsItemSelected() 并将其放入正确的大小写中,这样它应该可以工作。

      【讨论】:

        【解决方案4】:

        为什么你有一个名为 onOptionsItemSelected*1* 的单独方法?

        所有对操作项做出反应的代码都应该在 onOptionsItemSelected 中:

        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);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-10-26
          • 2014-01-08
          • 1970-01-01
          • 1970-01-01
          • 2012-08-06
          • 1970-01-01
          • 2021-09-22
          • 1970-01-01
          • 2015-03-15
          相关资源
          最近更新 更多