启动在android中发送邮件的活动

时间:2012-11-02 03:35:02

标签: android android-intent

我正在设计一个发送邮件的应用程序。全部设置但是当代码到达emmulator中的以下行时,它表示“应用程序已意外停止”并且日志cat显示NullPointerException。我也发现了尽可能多的权限。 Plz帮助我了解清单文件中必须给出的具体权限以及如何解决问题。

startActivity(Intent.createChooser(send, "This is the chooser title"));

发送是我的意图。

完整的log cat msg如下:

11-01 23:21:37.721: W/IInputConnectionWrapper(442): showStatusIcon on inactive InputConnection
11-01 23:21:39.781: I/msg(442): this is offhook
11-01 23:21:43.991: I/msg(442): this is idle
11-01 23:21:43.991: I/msgfinal(442): this is it
11-01 23:21:43.991: I/msg(442): this is from msg
11-01 23:21:43.991: I/sha(442): here
11-01 23:21:43.991: D/AndroidRuntime(442): Shutting down VM
11-01 23:21:43.991: W/dalvikvm(442): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-01 23:21:43.991: E/AndroidRuntime(442): FATAL EXCEPTION: main
11-01 23:21:43.991: E/AndroidRuntime(442): java.lang.NullPointerException
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.app.Activity.startActivityForResult(Activity.java:2827)
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.app.Activity.startActivity(Activity.java:2933)
11-01 23:21:43.991: E/AndroidRuntime(442):  at com.example.dialing.MainActivity.fun(MainActivity.java:33)
11-01 23:21:43.991: E/AndroidRuntime(442):  at com.example.dialing.PhoneCallListener.onCallStateChanged(MainActivity.java:104)
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.os.Looper.loop(Looper.java:123)
11-01 23:21:43.991: E/AndroidRuntime(442):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-01 23:21:43.991: E/AndroidRuntime(442):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 23:21:43.991: E/AndroidRuntime(442):  at java.lang.reflect.Method.invoke(Method.java:507)
11-01 23:21:43.991: E/AndroidRuntime(442):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-01 23:21:43.991: E/AndroidRuntime(442):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-01 23:21:43.991: E/AndroidRuntime(442):  at dalvik.system.NativeStart.main(Native Method)
11-01 23:21:48.481: I/Process(442): Sending signal. PID: 442 SIG: 9

Plz注意到“这里”(第6行),它是Log。我给它进行检查,它就是startActivity函数之前的行。

        Intent msg=new Intent(Intent.ACTION_SEND);
        String[] recipients={"myid@gmail.com"};

        msg.putExtra(Intent.EXTRA_EMAIL, recipients);

        msg.putExtra(Intent.EXTRA_TEXT, "This is the email body");
        msg.putExtra(Intent.EXTRA_SUBJECT, "This is the email subject");

        //msg.setType("message/rfc822");
        msg.setType("*/*");

        //context.startActivity(Intent.createChooser(msg, "This is the chooser title"));
        Log.i("msg","this is from msg");

        //calling into main activity
        MainActivity ma=new MainActivity();
        ma.fun(msg);

//此函数位于mainActivity

public void fun(Intent send)
{
    Log.i("sha","here");
    startActivity(Intent.createChooser(send, "This is the chooser title"));

    Log.i("sha","here2");
}

4 个答案:

答案 0 :(得分:5)

走这条路:

Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL,new String[] { "rahul@mail.com"});
    email.putExtra(Intent.EXTRA_SUBJECT,"Contact Us");
    email.putExtra(Intent.EXTRA_TEXT,"sent a message using the contact us ");

    email.setType("message/rfc822");

    startActivityForResult(Intent.createChooser(email, "Choose an Email client:"),
                        1);

然后创建方法onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == MY_REQUEST_CODE) {
        if(resultCode == RESULT_OK) {


        } else {

            Intent ingoHome = new Intent(abc.this,
                    pqr.class);
            ingoHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(ingoHome);


        }
    }

    finish();

}

答案 1 :(得分:1)

试试这个。 。 。 。 。它工作正常。

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{""}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,""); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,""); 
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

如果您需要将另一个文件附加到电子邮件中,可以将其添加到“email Intent.putExtra();”

答案 2 :(得分:0)

问题在于您使用的上下文。代码很好。只是不要使用

MainActivity ma=new MainActivity();
ma.fun(msg);

一样使用它
MainActivity.this.fun(msg);

答案 3 :(得分:0)

java.lang.NullPointerException 基本上意味着您正在引用一个不存在的对象(尚未。)

尝试注释掉或删除这些行:

MainActivity ma=new MainActivity();
ma.fun(msg);

用MainActivity.fun(Intent msg)方法中的代码替换它们,匹配变量名称:

Log.i("sha","here");
startActivity(Intent.createChooser(msg, "This is the chooser title"));
Log.i("sha","here2");

如果我正确阅读您的代码,您一直在尝试创建当前Activity的另一个实例,并为其传递Intent,其中包含您当前Context的{​​{1}} 1}}。您已经在Activity的实例中(它已由Android实例化)。

编辑:startActivity和startActivityforResult都启动另一个Activity,startActivityforResult期望结果返回到第一个Activity中的回调。我将努力让startActivity首先工作,如果需要,你总是可以将它转换为startActivityforResult。

当您的应用程序在MainActivity崩溃时,似乎startActivity在实例化之前正在接收“发送”。

我只是回应其他所有人,并说你的代码应该可以工作..只要你的巨大的上下文代码块是调用你的fun()方法的唯一地方。如果你把它全部放在自己的方法中,它会是什么样子:

MainActivity.fun(MainActivity.java:33)