自动呼叫邮件发件人

时间:2014-02-05 07:11:08

标签: java android nullpointerexception

基本上我正在制作一个移动取景器应用程序,这里是强盗的周围声音。当我发送消息时,被盗的手机应该自动给我打电话

@Override
public void onReceive(Context context, Intent intent) {


    final Bundle bundle = intent.getExtras();

    // try {

    if (bundle != null) {

        final Object[] pdusObj = (Object[]) bundle.get("pdus");

        for (int i = 0; i < pdusObj.length; i++) {

            SmsMessage currentMessage = SmsMessage
                    .createFromPdu((byte[]) pdusObj[i]);
            String phoneNumber = currentMessage
                    .getDisplayOriginatingAddress();

            String senderNum = phoneNumber;
            String message = currentMessage.getDisplayMessageBody();

            if (message.contains("mfmic")) {
                                  Intent dial = new Intent(Intent.ACTION_CALL);
                  dial.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  dial.setData(Uri.parse("tel:" + phoneNumber));
                  context.startActivity(dial);
                              }
                       }}}

错误LOGCAT:在for循环中出现空指针

02-05 12:35:11.842: E/AndroidRuntime(631): FATAL EXCEPTION: main
02-05 12:35:11.842: E/AndroidRuntime(631): java.lang.RuntimeException: Unable to start receiver com.example.mobilefinder.Services: java.lang.NullPointerException
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.os.Looper.loop(Looper.java:123)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-05 12:35:11.842: E/AndroidRuntime(631):  at java.lang.reflect.Method.invokeNative(Native Method)
02-05 12:35:11.842: E/AndroidRuntime(631):  at java.lang.reflect.Method.invoke(Method.java:507)
02-05 12:35:11.842: E/AndroidRuntime(631):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-05 12:35:11.842: E/AndroidRuntime(631):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-05 12:35:11.842: E/AndroidRuntime(631):  at dalvik.system.NativeStart.main(Native Method)
02-05 12:35:11.842: E/AndroidRuntime(631): Caused by: java.lang.NullPointerException
02-05 12:35:11.842: E/AndroidRuntime(631):  at com.example.mobilefinder.Services.onReceive(Services.java:91)
02-05 12:35:11.842: E/AndroidRuntime(631):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
02-05 12:35:11.842: E/AndroidRuntime(631):  ... 10 more

2 个答案:

答案 0 :(得分:0)

您从bundle获取的Object []为null final Object[] pdusObj = (Object[]) bundle.get("pdus");请重新检查。

答案 1 :(得分:0)

您可以使用此代码

public class TextMessageReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
              }
        for (SmsMessage msg : sms) {
                  //message body 'msg.getMessageBody()'
                  //message sender 'msg.getOriginatingAddress()'
                  String number = "tel:" + msg.getOriginatingAddress();
              Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse(number));
              startActivity(callIntent);
              }
        }