2

I am designing an application for sending mail. All is set but when the code reaches to the following line in the emmulator it says "application has stopped unexpectedly" and the log cat shows NullPointerException. I have also given as many permissions i found. Plz help me with what specific permissions must be given in manifest file and how can the problem be resolved.

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

Send is my intent here.

Complete log cat msg is below:

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 note that upto "here"(6th line), it is Log. I which i gave for checking and it is the line just before the startActivity function.

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

//this function is inside the mainActivity

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

    Log.i("sha","here2");
}
benny Mr.
  • 57
  • 1
  • 3
  • 10

4 Answers4

5

Go this way:

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

and then create methos 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();

}
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46
  • what is MY_REQUEST_CODE,RESULT_OK and which class to use for pqr? – benny Mr. Nov 02 '12 at 04:24
  • `private static final int MY_REQUEST_CODE = 1;` – Rahul Patel Nov 02 '12 at 04:41
  • and `RESULT_OK` is the standard activity result. – Rahul Patel Nov 02 '12 at 04:46
  • i have problem with pqr.class---whats that, i mean which class to specify there,my program has a single class and thats MainActivity. i also tried giving both the classes as mainactivity but it also doesnt work.:( – benny Mr. Nov 02 '12 at 05:02
  • If you have a single activity,then do not need to use `startActivityForResult` you can use `startActivity`.check this link, http://stackoverflow.com/a/12091789/1263679 – Rahul Patel Nov 02 '12 at 05:30
  • and one more thing,if you are running on emulator then you have email application in your emulator, for that check this link http://stackoverflow.com/a/6978974/1263679 – Rahul Patel Nov 02 '12 at 05:33
  • i created a dummy activity and now it is working (thought it is not exactly what i wanted...but ok) thanks a lot..... – benny Mr. Nov 02 '12 at 13:37
  • @bennyMr. If it helps you then please give upvote and accept answer. – Rahul Patel Nov 03 '12 at 03:53
1

Try this . . . . . it works fine .

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..."));

if you need to attach anyother file to the email , you could add it in "emailIntent.putExtra();"

VIGNESH
  • 2,023
  • 8
  • 31
  • 46
  • i also used the same code...i have updated it now in my ques. plz have a look and suggest what is the problem. – benny Mr. Nov 02 '12 at 04:02
0

The problem is the context you use. The code is just fine. just dont use

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

use it like

MainActivity.this.fun(msg);
san
  • 1,845
  • 13
  • 23
0

A java.lang.NullPointerException basically means you are referencing an object that doesn't exist (yet.)

Try commenting out or deleting these lines:

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

And replacing them with the code from your MainActivity.fun(Intent msg) method, matching the variable name:

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

If I'm reading your code correctly, you have been trying to create another instance of your current Activity and pass it an Intent that contains the Context for your current Activity. You were already in an instance of MainActivity (it was instantiated by Android for you.)

EDIT: startActivity and startActivityforResult both launch another Activity, with startActivityforResult expecting a result to be returned to the callback in the first Activity. I'd work on getting startActivity to work first, you can always convert it to startActivityforResult if you need to.

As your app was crashing at MainActivity.fun(MainActivity.java:33), it seems likely that startActivity is receiving "send" before it has been instantiated.

I'm just going to echo everyone else, and say your code should work .. as long as your huge out-of-context code block is the only place calling your fun() method. Here's what it would look like if you put it all in its own method:

class MainActivity extends Activity {
    private static final String TAG = Controller.class.getSimpleName();

    /* fields, other methods */

    private void sendMail(){
        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");
        // I personally use application/octet-stream because it
        // shows all my email clients, but not much other useless stuff.
        // Might get me flamed, though lol :)
        msg.setType("application/octet-stream");
        MainActivity.this.getApplicationContext().startActivity(
            Intent.createChooser(msg, "This is the chooser title")
        );
        Log.i(TAG,"made it! :D");
    }/*end sendMail()*/

    /* more fields and methods */

}/*end MainActivity*/
CodeShane
  • 6,480
  • 1
  • 18
  • 24
  • Well, your logcat doesn't match your code sample .. looks like you switched from "startActivity" to "startActivityForResult" .. are you still getting the same NullPointerException? – CodeShane Nov 02 '12 at 05:10
  • yup seening the error in the logcat i thought creating startActivitfor result is required so i created it but was of no worth.so i removed it. now i dont have startActivitforresult method but getting the same error. logcat i showed above is the exact thing i m getting for whatever changes i m making. – benny Mr. Nov 02 '12 at 07:31
  • Updated answer, hope it helps. – CodeShane Nov 02 '12 at 08:20