使用android

时间:2015-05-12 17:03:28

标签: java android

如果我省略代码emailIntent.setType("text/plain");,为什么我的应用程序会在android中发现未找到活动的异常?

package com.example.app1;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Email extends Activity implements View.OnClickListener {
    EditText emailid, text1, text2, text3, text4;
    Button send;
    String temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        initializeViews();
        send.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {

        temp = text1.getText().toString() + text2.getText().toString()
                + text3.getText().toString() + text4.getText().toString();

        String[] id = { emailid.getText().toString() };
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, id);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, temp);
        emailIntent.setType("text/plain");

        try {
            startActivity(emailIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(Email.this,
                    "There are no email applications installed.",
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void initializeViews() {
        // TODO Auto-generated method stub
        emailid = (EditText) findViewById(R.id.editText1);
        text1 = (EditText) findViewById(R.id.editText2);
        text2 = (EditText) findViewById(R.id.editText3);
        text3 = (EditText) findViewById(R.id.editText4);
        text4 = (EditText) findViewById(R.id.editText5);
        send = (Button) findViewById(R.id.button1);

    }

}

我附上我的活动Java代码,请参阅onclick方法,所有必需的电子邮件代码都在其中。

1 个答案:

答案 0 :(得分:1)

尝试使用此方法

protected void sendEmail() {
          Log.i("Send email", "");

          String[] TO = {"mail.mail@gmail.com"};
          String[] CC = {"mail@gmail.com"};
          Intent emailIntent = new Intent(Intent.ACTION_SEND);
          emailIntent.setData(Uri.parse("mailto:"));
          emailIntent.setType("text/plain");


          emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
          emailIntent.putExtra(Intent.EXTRA_CC, CC);
          emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
          emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

          try {
             startActivity(Intent.createChooser(emailIntent, "Send mail..."));
             finish();
             Log.i("Finished sending email...", "");
          } catch (android.content.ActivityNotFoundException ex) {
             Toast.makeText(MainActivity.this, 
             "There is no email client installed.", Toast.LENGTH_SHORT).show();
          }
     }