Click here to Skip to main content
14,304,278 members
Rate this:
Please Sign up or sign in to vote.
Hi
Sir , I am new on Android platform and I have written a separate class for Sending SMS with confirmation code but when I send SMS through this then I face a problem that Your application has closed unexpectedly due to Null Pointer exception Actually I have send the sms from SettingScreen Activity through send_SMS method defined here sir pl help me I bind that code with the query in this regards

thanks
Om Parkash Kaushik
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.app.PendingIntent;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.app.Activity;
public class SendSMS extends Activity {
	private Context ctx;
	public SendSMS(Context object){
		this.ctx = object;
		}
	  //---sends an SMS message to another device---
	public void send_SMS(String phoneNumber, String message)
    { 
		try{
        String SENT = "SMS_SENT";
       // String DELIVERED = "SMS_DELIVERED";
 
       PendingIntent sentPI = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, new Intent(SENT),0);
    		   //getBroadcast( ctx, 0, new Intent(ctx,ctx.getClass()), 0);
      // PendingIntent deliveredPI = PendingIntent.getBroadcast( ctx, 0, new Intent(DELIVERED), 0);
 
       /// ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    	Toast.makeText(getBaseContext(), "SMS_SNT", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));
// 
//        //---when the SMS has been delivered---
//        registerReceiver(new BroadcastReceiver(){
//            @Override
//            public void onReceive(Context arg0, Intent arg1) {
//                switch (getResultCode())
//                {
//                    case Activity.RESULT_OK:
//                        Toast.makeText(getBaseContext(), "SMS delivered", 
//                                Toast.LENGTH_SHORT).show();
//                        break;
//                    case Activity.RESULT_CANCELED:
//                        Toast.makeText(getBaseContext(), "SMS not delivered", 
//                                Toast.LENGTH_SHORT).show();
//                        break;                        
//                }
//            }
//        }, new IntentFilter(DELIVERED));        
// 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, null);        
  
    }catch(Exception e){
    	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    }
	

}
Posted
Updated 4-Mar-12 23:22pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Change the following line...

catch(Exception e)

To

catch(Throwable e)

Once you do that you will get an exception (presuming the code you have there works.)
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100