无法启动接收器NullPointerException

时间:2013-10-31 17:31:49

标签: java android

我有一个接收短信的BroadcastReceiver,如果文本有某些字符,我应该立即发送回复短信。现在它接收并发送短信,但很快就会强行关闭给出类似的东西:无法启动接收器... NullPointerException ... LogCat中的ActivityThread.handleReciever(释义)......可能是什么问题?这是我的代码,您正在寻找ELSE IF语句(这是我目前正在测试的部分):

public class Service extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction()
            .equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras(); 
        SmsMessage[] msgs = null;
        String msgFrom;
        if (bundle != null) {
            try {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for (int i = 0; i < msgs.length; i++) {
                    String verificationCode = "717345221";
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    msgFrom = msgs[i].getOriginatingAddress();
                    String encodeHash = Uri.encode("#");
                    msgFrom = "0" + msgFrom.substring(4) + encodeHash;
                    String msgBody = msgs[i].getMessageBody();

                    if (msgBody.startsWith(verificationCode)) {
                        this.abortBroadcast();
                        msgBody = msgBody.substring(verificationCode
                                .length());
                        try {
                            String dial = msgBody + "*" + msgFrom;
                            Intent call = new Intent(Intent.ACTION_CALL,
                                    Uri.parse("tel:" + dial));
                            call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(call);

                        } catch (Exception e) {
                            e.toString();
                        }
                    } else if (msgBody.contains("TEST123")) {
                        Toast.makeText(context,
                                "TEST123 text recieved",
                                Toast.LENGTH_LONG).show();
                        String transactionCode = null;
                        if (msgBody.length() > 9) {
                            transactionCode = msgBody.substring(0, 9);
                        }

                        String attachCode = "776f76wfuh";
                        String number = msgs[i].getOriginatingAddress();
                        String message = attachCode + transactionCode;

                        try {
                            SmsManager sms = SmsManager.getDefault();
                            PendingIntent pi = PendingIntent.getBroadcast(
                                    context, 0, new Intent(context,
                                            Service.class), 0);
                            sms.sendTextMessage(number, null, message, pi,
                                    null);
                            Toast.makeText(context,
                                    "Text sent with attach code",
                                    Toast.LENGTH_LONG).show();

                        } catch (Exception e) {
                            // TODO: handle exception
                            Log.d("Exception caught", e.getMessage());
                        }
                    }
                }
            } catch (Exception e) {
                Log.d("Exception caught", e.getMessage());
            }
        }
    }
}
}

这是LogCat:

10-31 20:42:05.269: E/AndroidRuntime(15080): FATAL EXCEPTION: main
10-31 20:42:05.269: E/AndroidRuntime(15080): java.lang.RuntimeException: Unable to start receiver com.adbionicpaymentsystem.Service: java.lang.NullPointerException
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.app.ActivityThread.access$2400(ActivityThread.java:117)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.os.Looper.loop(Looper.java:130)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.app.ActivityThread.main(ActivityThread.java:3683)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at java.lang.reflect.Method.invoke(Method.java:507)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at dalvik.system.NativeStart.main(Native Method)
10-31 20:42:05.269: E/AndroidRuntime(15080): Caused by: java.lang.NullPointerException
10-31 20:42:05.269: E/AndroidRuntime(15080):    at com.adbionicpaymentsystem.Service.onReceive(Service.java:21)
10-31 20:42:05.269: E/AndroidRuntime(15080):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
10-31 20:42:05.269: E/AndroidRuntime(15080):    ... 10 more

2 个答案:

答案 0 :(得分:0)

确保您的清单中有权限

<uses-permission android:name="android.permission.SEND_SMS"/>

并注意 sendTextMessage

This method was deprecated in API level 4.
Use android.telephony.SmsManager.

答案 1 :(得分:0)

问题是我在测试设备上运行了另一项看似干扰此服务的服务