发送短信时崩溃(NullPointerException)我该怎么办?

发布于 2024-12-12 03:00:26 字数 6791 浏览 0 评论 0原文

我有一个应用程序可以向其他 Android 或非 Android 手机发送短信。您可以选择或输入您的联系人。但问题是,当我到达发送短信的部分时,它不断崩溃。

btnRecipient.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
                startActivityForResult(intent, CONTACT_PICKER_RESULT);
            }
        });
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (CONTACT_PICKER_RESULT):
                if (resultCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = managedQuery(contactData, null, null, null, null);
                    if (c.moveToFirst()) {
                        String name = c.getString(c.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
                        editTextRecipient.setText(name);
                    }
                }
                break;
        }

        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 

                if (phoneNo.length()>0 && message.length()>0)                
                    sendSMS(phoneNo, message);                
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    private void sendSMS(String phoneNo, String message)
    {      
        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);

        registerReceiver(new BroadcastReceiver(){
            @Override
            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;
                }
            }
        }, new IntentFilter(SENT));

        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(phoneNo, null, message, sentPI, deliveredPI);               
    }   
}

这是 LogCat:

10-26 12:23:29.633: E/AndroidRuntime(11986): FATAL EXCEPTION: main
10-26 12:23:29.633: E/AndroidRuntime(11986): java.lang.NullPointerException
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.telephony.gsm.SmsMessage.getSubmitPduHead(SmsMessage.java:663)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(SmsMessage.java:339)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsMessage.getSubmitPdu(SmsMessage.java:613)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:228)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:107)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.gsm.SmsManager.sendTextMessage(SmsManager.java:79)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp.sendSMS(KAHTextApp.java:136)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp.access$0(KAHTextApp.java:78)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp$2.onClick(KAHTextApp.java:69)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.view.View.performClick(View.java:2532)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.view.View$PerformClick.run(View.java:9293)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Handler.handleCallback(Handler.java:587)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Looper.loop(Looper.java:143)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.app.ActivityThread.main(ActivityThread.java:4277)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at java.lang.reflect.Method.invokeNative(Native Method)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at java.lang.reflect.Method.invoke(Method.java:507)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at dalvik.system.NativeStart.main(Native Method)

这里有什么问题?如何解决这个问题?

I have an app that send sms to other android or non-android phones. You can select or input your contacts. But the problem is, that it keeps crashing when I get to the part where it sends SMS.

btnRecipient.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
                startActivityForResult(intent, CONTACT_PICKER_RESULT);
            }
        });
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (CONTACT_PICKER_RESULT):
                if (resultCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = managedQuery(contactData, null, null, null, null);
                    if (c.moveToFirst()) {
                        String name = c.getString(c.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
                        editTextRecipient.setText(name);
                    }
                }
                break;
        }

        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 

                if (phoneNo.length()>0 && message.length()>0)                
                    sendSMS(phoneNo, message);                
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    private void sendSMS(String phoneNo, String message)
    {      
        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);

        registerReceiver(new BroadcastReceiver(){
            @Override
            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;
                }
            }
        }, new IntentFilter(SENT));

        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(phoneNo, null, message, sentPI, deliveredPI);               
    }   
}

and heres the LogCat:

10-26 12:23:29.633: E/AndroidRuntime(11986): FATAL EXCEPTION: main
10-26 12:23:29.633: E/AndroidRuntime(11986): java.lang.NullPointerException
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.telephony.gsm.SmsMessage.getSubmitPduHead(SmsMessage.java:663)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.telephony.gsm.SmsMessage.getSubmitPdu(SmsMessage.java:339)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsMessage.getSubmitPdu(SmsMessage.java:613)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:228)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:107)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.telephony.gsm.SmsManager.sendTextMessage(SmsManager.java:79)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp.sendSMS(KAHTextApp.java:136)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp.access$0(KAHTextApp.java:78)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.example.KAHTextApp.KAHTextApp$2.onClick(KAHTextApp.java:69)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.view.View.performClick(View.java:2532)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.view.View$PerformClick.run(View.java:9293)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Handler.handleCallback(Handler.java:587)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.os.Looper.loop(Looper.java:143)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at android.app.ActivityThread.main(ActivityThread.java:4277)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at java.lang.reflect.Method.invokeNative(Native Method)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at java.lang.reflect.Method.invoke(Method.java:507)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-26 12:23:29.633: E/AndroidRuntime(11986):    at dalvik.system.NativeStart.main(Native Method)

what is the problem here? and how can to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-12-19 03:00:26

这看起来与描述的问题非常相似 这里

如果将方法调用 sendTextMessage 替换为 sendMultipartTextMessage 是否有效?您能记录此时可变消息的长度吗?

编辑:

所以看来,如果您尝试发送超过 160 个字符的一条短信,则会出现空指针异常。为了避免这种情况,请将消息分成多个部分。这是我编写的一些测试代码:

public void textSingle(View view) {
    sendSMS("XXXXXXXXXX", "Test", false);
}

public void textSingleLong(View view) {
    sendSMS("XXXXXXXXXX",
            "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest",
            false);
}

public void textMulti(View view) {
    sendSMS("XXXXXXXXXX",
            "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest",
            true);
}

private void sendSMS(String phoneNo, String message, boolean split) {
    ...

    SmsManager sms = SmsManager.getDefault();
    if (!split) {
        Log.d("SMSTest", "Sending single message: " + message);
        sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
    } else {
        Log.d("SMSTest", "Sending '" + message + "' in multiple parts.");
        ArrayList<String> parts = sms.divideMessage(message);
        Log.d("SMSTest", parts.size() + " parts:");
        for (String string : parts) {
            Log.d("SMSTest", string);
        }
        ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
        for (int i = 0; i < parts.size(); i++) {
            sentList.add(sentPI);
            deliveredList.add(deliveredPI);
        }
        sms.sendMultipartTextMessage(phoneNo, null, parts, sentList,
                deliveredList);
    }
}

我将按钮连接到每个方法,它们对我来说工作正常,当我尝试发送一条长消息时,会出现 NullPointerException。我真的不知道为什么当你这样做时会收到如此无益的消息,但似乎就是这样。

This looks very similar to the issue described here.

If you replace the method call sendTextMessage with sendMultipartTextMessage does it work? Can you log how long your variable message is at that point?

EDIT:

So it seems that if you try to send a single sms message that exceeds 160 characters, you get a null pointer exception. To avoid this, split the message into multiple parts. Here is some test code I wrote:

public void textSingle(View view) {
    sendSMS("XXXXXXXXXX", "Test", false);
}

public void textSingleLong(View view) {
    sendSMS("XXXXXXXXXX",
            "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest",
            false);
}

public void textMulti(View view) {
    sendSMS("XXXXXXXXXX",
            "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest",
            true);
}

private void sendSMS(String phoneNo, String message, boolean split) {
    ...

    SmsManager sms = SmsManager.getDefault();
    if (!split) {
        Log.d("SMSTest", "Sending single message: " + message);
        sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
    } else {
        Log.d("SMSTest", "Sending '" + message + "' in multiple parts.");
        ArrayList<String> parts = sms.divideMessage(message);
        Log.d("SMSTest", parts.size() + " parts:");
        for (String string : parts) {
            Log.d("SMSTest", string);
        }
        ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
        for (int i = 0; i < parts.size(); i++) {
            sentList.add(sentPI);
            deliveredList.add(deliveredPI);
        }
        sms.sendMultipartTextMessage(phoneNo, null, parts, sentList,
                deliveredList);
    }
}

I hooked up buttons to each of these methods, and they work correctly for me, with a NullPointerException when I try to send a single long message. I don't really know why you get such an unhelpful message when you do that, but that seems to be how it is.

不疑不惑不回忆 2024-12-19 03:00:26

一行中完成:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

Do it in one line:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文