-1

Im developing a call blocking application but i faced with this error. The error is attached. Would you please help me out ?

public class IncomingCall extends BroadcastReceiver {
Context context;
@Override
public void onReceive(Context context, Intent intent) {
    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        //Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    } catch (Exception e) {
        Log.e("Phone  Receive Error", " " + e);
    }
}
    private class MyPhoneStateListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
             Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);
            // state = 1 means when phone is ringing
            if (state == 1) {
                String msg = " New Phone Call Event. Incomming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText( context, msg,duration);
                toast.show();
            }
        }
    }
}

Error

Vahid
  • 1,258
  • 1
  • 14
  • 14
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) ...obviously it was never assigned ... so what did you expect? – Selvin Jul 28 '15 at 12:53

1 Answers1

1

You're encountering this error because "context" in "Toast.makeText(context, msg, duration);" is null. You need to set the class's "context" field in your onReceive method:

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
Ben Bishop
  • 1,414
  • 9
  • 14