• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps sending notification

NuffsaidM8

Newbie
Nov 9, 2015
18
0
I've read many tutorials and none of them are making full sense to me. The one I tried had almost no instruction whatsoever, but seemed far simpler than the others. Here is the important code right now:

Code:
public void startAlarm(View view) {
        Intent intent = new Intent(this, AlarmReciever.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
    }

This is run when a button on the only activity on the app is clicked. The AlarmReciever class looks like this:

Code:
@Override
public void onReceive(Context context, Intent intent) {
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(App.getAppContext())
        .setContentText("The time has come for the notification to trigger...")
        .setSmallIcon(R.drawable.notificationicon)
        .setLights(Color.rgb(78, 155, 222), 1000, 1000)
        .setAutoCancel(true)
        .setContentTitle("Your alarm is triggered!");
    NotificationManager manager = (NotificationManager) App.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1, builder.build());
}

The App.getAppContext() was just to retrieve the app context. The class App looks like this:
Code:
public class App extends Application {

    private static Context mContext;

    @Override public void onCreate(){
        super.onCreate();

        App.mContext = getApplicationContext();
    }

    public static Context getAppContext(){
        return App.mContext;
    }

}

What I want is for the notification and the vibrator in the AlarmReciever class to trigger immediately when the button is clicked. Yes it's useless, it's just for testing purposes.

What happens though is nothing for a few seconds, then the app crashes while displaying the app stopped running message. The odd thing is the vibrator continues to vibrate the phone until I click ok on the app crashing notification.

Can anyone explain this behavior and what I need to fix to avoid it?

BTW, this is the tutorial: http://www.compiletimeerror.com/2013/10/alarm-example-in-android.html?m=1
 
12-29 18:59:09.000 27594-27594/eli.me.jitteralarm E/AndroidRuntime: FATAL EXCEPTION: main
Process: eli.me.jitteralarm, PID: 27594
java.lang.RuntimeException: Unable to start receiver eli.me.jitteralarm.AlarmReciever: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:27)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I'm not sure how the App class is null here.

I asked for help on StackOverflow and that is where the workaround to get the context is from. He also mentioned that I might need to point the application tag in the manifest.xml file to the App class. Is that the issue?
 
Upvote 0
Here's the relevant part of the stack trace which tells you what the problem is

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:27)

So I assume line 27 is this

Code:
    NotificationManager manager = (NotificationManager) App.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);

So it looks like you called getSystemService with a null parameter. I suspect you did not initialise Context.NOTIFICATION_SERVICE.

The way to investigate these sort of problems is to run your app in debug mode. Then set a breakpoint in your code, which will suspend execution at that line. You can then examine the values of variables, and this will help you understand what's causing the problem.
 
Upvote 0
Here's the relevant part of the stack trace which tells you what the problem is

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:27)

So I assume line 27 is this

Code:
    NotificationManager manager = (NotificationManager) App.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);

So it looks like you called getSystemService with a null parameter. I suspect you did not initialise Context.NOTIFICATION_SERVICE.

The way to investigate these sort of problems is to run your app in debug mode. Then set a breakpoint in your code, which will suspend execution at that line. You can then examine the values of variables, and this will help you understand what's causing the problem.
The error says that the object reference is null. Wouldn't this mean that the object I am invoking the getSystemService method on is null, not the parameter?

Although if I am making a mistake, how would I properly initialize the parameter?
 
Upvote 0
Activity inherits from Context. Can you not just call getAppContext in your main Activity class, and call a setter method on your AlarmReceiver class to store as a class variable.
Alternatively define a constructor method which takes an ApplicationContext as a parameter.

Then in your onReceive method you can use the class variable to get the system service.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones