1

I am building an app to send sms and receive delivery reports. I have a button on the form with onclick attribute of m1, when I click the button the app crashes. What is it I am doing wrong?

This is the code:

public class m1g extends AppCompatActivity {
    private BroadcastReceiver sendBroadcastReceiver;
    private BroadcastReceiver deliveryBroadcastReceiver;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m1g);


        TextView rec1 = (TextView) findViewById(R.id.rec1);
        TextView pawn = (TextView) findViewById(R.id.pawn);
        TextView paw = (TextView) findViewById(R.id.paw);
        TextView paa = (TextView) findViewById(R.id.paa);
        TextView ob = (TextView) findViewById(R.id.ob);
        TextView re1 = (TextView) findViewById(R.id.re1);

        Intent i = getIntent();
        Intent ii = getIntent();
        Intent iii = getIntent();
        ob.setText(ii.getStringExtra("tex"));
        rec1.setText(i.getStringExtra("text"));
        re1.setText(iii.getStringExtra("te"));


        pawn.setPaintFlags(pawn.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        paw.setPaintFlags(paw.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        paa.setPaintFlags(paa.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);


        sendBroadcastReceiver = new BroadcastReceiver() {

            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Sent", 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;
                }
            }
        };

        deliveryBroadcastReceiver = new BroadcastReceiver() {
            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;
                }
            }
        };
        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
    }

        public void m1(){
            String phoneNumber = "08039123061";
            String message = "mad";
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";
            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
        }
    @Override
    protected void onStop()
    {
        unregisterReceiver(sendBroadcastReceiver);
        unregisterReceiver(deliveryBroadcastReceiver);
        super.onStop();
    }

}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • 4
    Examine LogCat to view the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 03 '15 at 21:00
  • not very close to my computer now . can you examine the code to see if something is wrong ? – Oyubu Obukohwo Dec 03 '15 at 21:23

2 Answers2

1

Check your manifest and add this permissions

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
Context
  • 1,873
  • 1
  • 20
  • 24
1

You've got the wrong signature for the m1() method. Its declaration should be:

public void m1(View v)
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thanks you saved me . it dosent crash anymore but message dosent send it just shows generic failiour , what could be wrong – Oyubu Obukohwo Dec 04 '15 at 05:54
  • Ugh, that's the worst one to diagnose. It's rather difficult to tell what the cause could be. You might be having a problem with the phone number format. Try adding a `+1` at the beginning. If that doesn't work, try whatever other formats are valid in your region; e.g., if you can drop any leading zeroes, or if you can drop the "area code", etc. You might compare your number to however your stock messaging app formats it when it sends a message. – Mike M. Dec 04 '15 at 06:01
  • I DON'T KNOW WHAT JUST WANT WRONG. suddenly my phone can't send messages , I tried sending with the native messaging app and I get message not sent on both networks , my phone is a dual sim and I get this error on both sim. what could have gone wrong suddenly – Oyubu Obukohwo Dec 04 '15 at 07:21
  • I have no idea. That sounds like either a network or a hardware problem. We can't really help you with those here, as it would just be speculation on our part, but make sure you're getting a good signal, and that you don't have it in airplane mode. Having the radio off can sometimes just give a generic failure. If still nothing, you should call your provider, and double check your SMSC (Service Center) settings. – Mike M. Dec 04 '15 at 07:28
  • it now works for reasons I don't know . is it possible to change the toast message with a debug alert? – Oyubu Obukohwo Dec 04 '15 at 08:30
  • I'm sorry, I don't know what you mean by "debug alert". – Mike M. Dec 04 '15 at 08:32
  • I meant replacing the toast alert with Alertdialog – Oyubu Obukohwo Dec 04 '15 at 11:36
  • Sure. Here's a simple example: `new AlertDialog.Builder(context).setNeutralButton("OK", null).setMessage("SMS sent").show();` – Mike M. Dec 04 '15 at 11:43