1

I am new to Android. Here, I am not getting any errors ,While debugging the stopAlarm() method debugger crossed all the lines but the AlarmReceiver is not get called .

can anyone help me to fix it .

Update :

AlarmActivity.java

public void stopAlarm(Context context) {
        Intent intent = new Intent(context,AlarmReceiver.class);
        intent.setAction("ALARM_OFF");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mAlarmId, intent,0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }
Zhu
  • 3,679
  • 14
  • 45
  • 76
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Raj Jul 18 '18 at 15:10
  • 1
    can you help me to fix this issue ,I can't understand how my issue is same to the post you mentioned @Raj – Zhu Jul 18 '18 at 15:12
  • 2
    It’s funny how every post that has `NullPointerException` in it gets flagged a duplicate of the post Raj linked. – 0xCursor Jul 18 '18 at 15:38
  • 1
    How are you setting `alarmActivity` in `WakeupScreen`? Add the code to your original post (edit it please). – David Wasser Jul 18 '18 at 16:14
  • 2
    @LAD This is exactly the type of situation where declaring this a duplicate of the cononical "What is a NullPointerException" is not helpful. OP isn't calling any method on a null variable. The Android framework is doing it. And the OP has no clue why or where the framework is doing this and how he can fix it. OP is hopelessly lost unless he reads the Android source code for 2 years or gets someone else to help. Closing this as a duplicate is unhelpful at best and rude at worst. See https://meta.stackoverflow.com/questions/255062/nullpointerexception-question-on-stack-overflow/340576#340576 – David Wasser Jul 18 '18 at 16:19
  • @David Wasser Yeah, exactly right. It is so common, though. Too often posts get flagged as duplicate but the linked post doesn’t exactly explain the unique situation, it is just a general solution. – 0xCursor Jul 18 '18 at 17:01
  • 1
    @LAD This is unfortunately a problem because the moderators are overloaded with flags. If someone marks it as "duplicate", the moderators need to decide what to do, and often they don't have time to dig through the post or they aren't familiar enough with the programming language or the particular framework or library. I try to help as much as I can by going through Android questions that are flagged as "duplicate", but I probably am not making a dent in the backlog. I have a day job and company to run and kids and dogs waiting at home :-( – David Wasser Jul 18 '18 at 17:14
  • https://stackoverflow.com/questions/56755169/android-studio-null-by-reference-on-click-need-help-debugging anyone can help me in this qn @DavidWasser – Zhu – Wei Hern Jason Lee Jun 25 '19 at 15:07

1 Answers1

3

The problem is here, in WakeUpScreen:

alarmActivity.stopAlarm();

You are calling the stopAlarm() method of the AlarmActivity() and in this case, AlarmActivity.this is null. I can only assume that you are doing somethng like this in WakeUpScreen:

alarmActivity = new AlarmActivity();

This is an absolute no-no! You cannot instantiate Android components (Activity, Service, BroadcastReceiver, Provider) using the keyword new. Only Android can create and initialize these components, because these components need to have their Context setup by the framework before they can be used.

If you want to call a method in another Activity, then you need to ensure that that method is static. If you declare your stopAlarm() method as static, you will find that it complains about a few things (like AlarmActivity.this) which is why you will need to rewrite the method to take a Context parameter, something like this:

public void stopAlarm(Context context) {
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(this.ALARM_SERVICE);
    alarmManager.cancel(alarmIntent);
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Bro ,Now the debugger crossed all the lines in the stopAlarm() method but AlarmReceiver class is not get called ,please help me to fix it bro ,I have updated the code I have currently working with @DavidWasser – Zhu Jul 22 '18 at 13:53
  • Remover the 'setAction()' call. You don't need that and it causes the 'cancel()' call to fail – David Wasser Jul 22 '18 at 21:08
  • Here I am using the service file based on the Action values that's why I am using ACTION ,Please see my updated service file . @DavidWasser – Zhu Jul 23 '18 at 14:00
  • Also I tried intent.putExtra() instead of setAction that also not worked @DavidWasser – Zhu Jul 23 '18 at 14:05
  • I have no idea what you mean by "the debugger crossed all the lines". Anyway, In your original question you were complaining about a `NullPointerException`, which I explained with my answer. Now you've got something completely different. I understood that you wanted to cancel a previously set alarm. I explained how to do that. Now what you are saying is that you want to call your `BroadcastReceiver` to stop some media playing. For that you don't need the `AlarmManager` at all. Just create your `Intent` and then call `sendBroadcast(intent)`. That will call your `BroadcastReceiver.onReceive()` – David Wasser Jul 23 '18 at 22:25
  • can you please help me to solve this https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity-in-android @DavidWasser – Zhu Jul 26 '18 at 15:18