Java.lang.RuntimeException接收BroadCast Intent时出错

时间:2015-01-23 21:28:08

标签: java android android-intent broadcastreceiver

我正在从我的具有用户界面

的活动中发送此类广播
//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();
}

但是当我在我的设备上执行此应用程序时,它停止工作。我收到错误&#34; 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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
        }