无法启动receiver null指针异常

时间:2015-07-10 10:47:03

标签: java android service sms broadcastreceiver

我试图检查服务是否正在运行。如果它正在运行,我确实想发送短信。不幸的是,当服务未运行时,应用程序崩溃,并且当服务运行时,短信不会发送。

logcat的:

  

E / SmsReciever:Exeption smsReceiverjava.lang.NullPointerException

CallHelper类中的stop方法:

 public boolean stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
   // mainActivity.toggleUI();

    return false;
}

 public class IncomingSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();
    private CallHelper callHelper;


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

        final Bundle bundle = intent.getExtras();

                if(callHelper.stop()) {

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


                                String sms = "TEST";
                                try {
                                    SmsManager smsManager = SmsManager.getDefault();
                                    smsManager.sendTextMessage(senderNum, null, sms, null, null);

                                    Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
                                }


                                Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);

                                // Show Alert
                                int duration = Toast.LENGTH_LONG;
                                Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                                toast.show();

                            } // End For loop
                        } // bundle is null

                    } catch (Exception e) {
                        Log.e("SmsReciever", "Exeption smsReceiver" + e);
                    }
                }
    }
}

更新 在初始化callhelper之后:

CallHelper callHelper = new CallHelper();

它现在在我的stop方法上给我一个nullpointerException。 我不知道为什么。它是一个返回true的布尔方法。

CallHelper Class:

public class CallHelper {

public Boolean calling = false;
private String phoneNumber ="0";
private String lastNumber;
private int count=0;


public CallHelper() {
}

// Listener to detect incoming calls.
public class  CallStateListener extends PhoneStateListener{
    @Override


    public  void onCallStateChanged (int state, String incomingNumber) {
        switch (state){...}

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private MainActivity mainActivity;

public CallHelper(Context ctx){
    this.ctx = ctx;

    callStateListener = new CallStateListener();
}

public void SendMessage(){...}

// Start Call Detection.
public void start() {
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

}

// Stop Call Dectection

public boolean stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
   // mainActivity.toggleUI();

    return true;
}

}

更新IncomingSMS类

公共类IncomingSms扩展了BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();
private CallDetectService callDetectService;

public IncomingSms(){}


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

    final Bundle bundle = intent.getExtras();

    callDetectService = new CallDetectService();
    Intent intent1 = new Intent(context,CallDetectService.class);

    if (!callDetectService.stopService(intent1)) {

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


                    String sms = "TEST";
                    try {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(senderNum, null, sms, null, null);

                        Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
                    }


                    Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);

                    // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();

                } // End For loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReciever", "Exeption smsReceiver" + e);
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

您的例外可能在以后发生,

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

听到捆绑包不应包含任何带有键的对象&#34; pdus&#34;所以pdusObj将保持为null并且在for循环中你试图通过使用&#34; pdusObj.length&#34;来获得长度。你得到java.lang.NullPointerException。