2

嗨,我的主要问题是向选定的目的地地址发送短信。大多不止一个。选择工作正常,但发送短信的方法总是显示 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!

4

1 回答 1

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

不管怎样,谢谢你帮助我!!

于 2012-10-01T09:21:02.140 回答