freeski_52
freeski_52

Reputation: 101

Android SMS to more than one destinationAdress throws NullPointerException

Hy, my major problem is to send a sms to selected destinationAdresses. Mostly more than one. The selection is working fine but the methode to send the sms always shows NullPointerException and I'm not able to locate the main error. The emerNames is a simple Array of Strings, the emerNumbers as well! The text to send in my mind isn't the problem but maybe the sending operation with the array in the for-loop!

Any suggestions?

        AlertDialog.Builder settingMessage = new AlertDialog.Builder(c);

    settingMessage.setTitle("SOS- Empfänger wählen");
    settingMessage.setMultiChoiceItems(emerNames, checkItem, new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) 
        {

            if (isChecked == true)
            {

                Log.i("AlertChoice", "SelectedItem: "+emerNames[which]);
                ischecked[which] = true;
                //emerNumbers[which]=emerNames[which];

            }

            else
            {
                ischecked[which]=false;
                //emerNumbers[which]= "";
                Log.i("AlertChoice", "Deseclected Item "+emerNames[which]);
            }

        }
    } );

    settingMessage.setNeutralButton("SOS senden", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {

            for (int i = 0; i<ischecked.length;i++)
            {
                if (ischecked[i]== true)
                {
                    Log.i("Selection", "SelectedItems:"+ emerNumbers[i]);
                    String text;
                    text = "HILFE, ich befinde mich in einer Notlage\n"
                            +"Meine persönlichen Daten:\n"
                            +"Vorname: "+empersData[0]+"\n"
                            +"Zuname: "+empersData[1]+"\n"
                            +"Alter: "+empersData[2]+"\n"
                            +"Blutgruppe: "+empersData[3]+empersData[4]+"\n";
                            /*+"Versicherungsnr: "+empersData[5]+"\n"
                            +"Geschlecht: "+empersData[6]+"\n";*/
                    SmsManager sms = SmsManager.getDefault();
                    Log.i("nr","Telefonnr 1: "+ emerNumbers[i]);
                    Log.i("text",text);
                    sms.sendTextMessage(emerNumbers[i], null, text, null, null);
                    //Toast.makeText(this, "SOS versendet", Toast.LENGTH_LONG).show();
                        Log.i("onClickTest", "Überprüfung Index: "+ischecked[i]);   

the methode "sms.sendTextMessage" throws the NullPointerException!

Upvotes: 2

Views: 137

Answers (1)

freeski_52
freeski_52

Reputation: 101

Okay, I just fixed the problem myself! The problem was the text for the message. Because the limit of messages is set in bounds of 160 characters. So, I adjusted the code by using the "SMSManger" and the Methode "sendMultipartTextMessage!! Below the added source code to make it work:

                            SmsManager sms = SmsManager.getDefault();
                    // Aufteilung des SMS Textes da mehr als 160 character --> sonst zu groß
                    //Splits the text in parts | divide Message (text)--> The Text to split
                    ArrayList<String> parts =sms.divideMessage(text);

                    int numParts = parts.size();

                    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
                    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

                    for (int j = 0; j < numParts; j++) 
                    {
                    sentIntents.add(PendingIntent.getBroadcast( c, 0, new Intent (), 0));
                    deliveryIntents.add(PendingIntent.getBroadcast(c, 0, new Intent (), 0));
                    }
                    sms.sendMultipartTextMessage(emerNumbers[i], null, parts, sentIntents, deliveryIntents);

Anyway, thanks for helping me!!

Upvotes: 1

Related Questions