我正在从具有UI的Activity中发送这样的广播

//Code in my UI Activity
        final String BROADCAST = "in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST" ;
        IntentFilter intentFilter = new IntentFilter("in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST");
        in.itechvalley.notification.MainActivity rec = new in.itechvalley.notification.MainActivity();
        Context context = getApplicationContext();
        context.registerReceiver(rec, intentFilter);
        Intent intent = new Intent(BROADCAST);
        context.sendBroadcast(intent);
        Log.d("MY TAG","Inside Broadcast");


我的清单声明:

        <receiver android:name="in.itechvalley.notification.MainActivity"
              android:exported="false" >
        <intent-filter >
             <action android:name="in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST"/>
        </intent-filter>
        </receiver>


我已经在onReceive()中编写了代码

@Override   //This is inside MainActivity Class that extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
{
    Log.d("MY TAG", "Inside the onReceive");
    Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DAY_OF_MONTH);
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    c.set(year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 3);
    c.set(Calendar.SECOND, 0);
    Toast.makeText(context, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}


但是,当我在设备上执行此应用程序时,它将停止工作。我收到错误消息“ java.lang.RuntimeException。。。。。我粘贴了LogCat

LOGCAT

01-24 02:43:01.320: E/AndroidRuntime(28802): FATAL EXCEPTION: main
01-24 02:43:01.320: E/AndroidRuntime(28802): Process: in.itechvalley, PID: 28802
01-24 02:43:01.320: E/AndroidRuntime(28802): java.lang.RuntimeException: Error receiving broadcast Intent { act=in.itechvalley.notification.MainActivity.android.ACTION.BROADCAST flg=0x10 } in in.itechvalley.notification.MainActivity@428d1de0
01-24 02:43:01.320: E/AndroidRuntime(28802):    at android.os.Handler.handleCallback(Handler.java:733)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at android.os.Looper.loop(Looper.java:136)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at android.app.ActivityThread.main(ActivityThread.java:5139)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at java.lang.reflect.Method.invoke(Method.java:515)

01-24 02:43:01.320: E/AndroidRuntime(28802):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at dalvik.system.NativeStart.main(Native Method)
01-24 02:43:01.320: E/AndroidRuntime(28802): Caused by: java.lang.NullPointerException
01-24 02:43:01.320: E/AndroidRuntime(28802):    at in.itechvalley.notification.MainActivity.onReceive(MainActivity.java:45)
01-24 02:43:01.320: E/AndroidRuntime(28802):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)


注意-我已经阅读了几乎所有与此相关的SO线程,但是找不到解决方案。我非常确定这是一个NPE,但是我不知道我在哪里得到NPE?

PS-MainActivity没有任何用户界面,而UIActivity具有用户界面,并且在启动应用程序时首先调用UIActivity

预先感谢您的帮助

更新

ScheduleClient.java

`public ScheduleClient(Context context) {
    mContext = context;
}

/**
 * Call this to connect your activity to your service
 */
public void doBindService() {
    // Establish a connection with our service
    mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

/**
 * When you attempt to connect to the service, this connection will be called with the result.
 * If we have successfully connected we instantiate our service object so that we can call methods on it.
 */
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established,
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};

/**
 * Tell our service to set an alarm for the given date
 * @param c a date to set the notification for
 */
public void setAlarmForNotification(Calendar c)
{
    mBoundService.setAlarm(c);
}

/**
 * When you have finished with the service call this method to stop it
 * releasing your connection and resources
 */
public void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        mContext.unbindService(mConnection);
        mIsBound = false;
    }
}

最佳答案

Logcat说您尝试在onServiceConnected调用之前设置日历(实际上是在与Service绑定的系统之前),这就是为什么它引发空指针异常的原因
onServiceConnected方法中添加setAlarmForNotification(Calendar c)调用

public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established,
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
       //call your setcalendar here
        }

09-10 16:24