Android SMS向多个destinationAdress抛出NullPointerException

时间:2012-09-30 11:02:15

标签: android

Hy,我的主要问题是向选定的目的地地址发送短信。大多不止一个。选择工作正常,但发送短信的方法总是显示NullPointerException,我无法找到主要错误。 emerNames是一个简单的字符串数组,emerNumbers也是! 在我脑海中发送的文本不是问题,但可能是for循环中数组的发送操作!

有什么建议吗?

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

方法“sms.sendTextMessage”抛出NullPointerException!

1 个答案:

答案 0 :(得分:1)

好的,我自己解决了这个问题! 问题是消息的文本。因为消息的限制设置为160个字符的边界。 所以,我使用“SMSManger”和Methode“sendMultipartTextMessage !!调整了代码! 在添加的源代码下方,使其工作:

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

无论如何,谢谢你的帮助!!