Search code examples
javaandroidandroid-intentbroadcastreceiver

Java.lang.RuntimeException Error Receiving BroadCast Intent


I am Sending A Broadcast like this from my Activity that has an UI

//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");

My Manifest Declaration:

        <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> 

And I've written a code in 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();
}

But when I am executing this app on my device, it Stops Working. I am getting an error "java.lang.RuntimeException....... I have pasted the 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)

NOTE- I have read almost all the SO threads regarding this but coundn't find the solution. I am pretty much sure that this is an NPE but I don't know where am I getting NPE ?

PS - MainActivity doesnt have any user interface whereas UIActivity has the user interface and UIActivity is called first when application is started

Thank You For Help In Advance

UPDATED

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

Solution

  • Logcat says you trying to set the Calender before onServiceConnected call(actually before your system bound with Service) thats why it throws null pointer exception .Add your setAlarmForNotification(Calendar c) call inside onServiceConnectedmethod

    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
            }