Intent对象不起作用

时间:2012-12-12 12:05:08

标签: android android-intent menu

我建了一个选项菜单。其中一个处理发送电子邮件的按钮无效。

以下是方法:

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_page, menu);
        return true;
    }

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings: Toast.makeText(this, "You pressed the settings!", Toast.LENGTH_LONG).show();
                                break;
            case R.id.exit:     System.exit(0); //close the program + kill it from memory
                                break;
            case R.id.contactUs:sendEmail();
                                break;
        }
        return true;
    }

private void sendEmail(){
        Intent mailIntent = new Intent();
        mailIntent.setAction(Intent.ACTION_SEND);
        mailIntent.setType("text/plain");
        mailIntent.putExtra(mailIntent.EXTRA_EMAIL, new String[]{"some_email@gmail.com"});
        mailIntent.putExtra(mailIntent.EXTRA_SUBJECT,"Re:Your Application");
    }

出于某种原因,我点击的任何项目(contactUs项目除外)都在工作。 当我点击contactUs项目时,它关闭菜单栏并且什么都不做......

需要帮助。

修改

我更改了以下内容:

private void sendEmail(){
    Intent mailIntent = new Intent();
    mailIntent.setAction(Intent.ACTION_SEND);
    mailIntent.setType("text/plain");
    mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"some_email@gmail.com"});
    mailIntent.putExtra(Intent.EXTRA_SUBJECT,"Re:Your Application");
    startActivity(Intent.createChooser(mailIntent, "some_email@gmail.com"));
}

字段仍未自动填充..

2 个答案:

答案 0 :(得分:5)

你必须致电

startActivity(Intent.createChooser(mailIntent, "Choose an Email client :"));

编辑:

更改这些行

mailIntent.putExtra(mailIntent.EXTRA_EMAIL, new String[]{"some_email@gmail.com"});
mailIntent.putExtra(mailIntent.EXTRA_SUBJECT,"Re:Your Application");

mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"some_email@gmail.com"});
mailIntent.putExtra(Intent.EXTRA_SUBJECT,"Re:Your Application");

编辑:

完整方法应如下所示:

private void sendEmail(){
    Intent mailIntent = new Intent();
    mailIntent.setAction(Intent.ACTION_SEND);
    mailIntent.setType("text/plain");
    mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"some_email@gmail.com"});
    mailIntent.putExtra(Intent.EXTRA_SUBJECT,"Re:Your Application");
    startActivity(Intent.createChooser(mailIntent, "Choose an Email client:"));
}

答案 1 :(得分:2)

您需要致电:

startActivity(Intent.createChooser(mailIntent,"Choose E-mail client:"));