android.content.res.resource android .content.context.getresource() 在空对象引用上

android.content.res.resource android .content.context.getresource() on a null object reference

我正在开发呼叫阻止应用程序,但我遇到了这个错误。 附上错误。 你能帮帮我吗?

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

您遇到此错误是因为 "Toast.makeText(context, msg, duration);" 中的 "context" 为空。您需要在 onReceive 方法中设置 class 的 "context" 字段:

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;