2

I have a notification set up in an activity. It creates the notification as expected. When I go back to the Home screen, the notification is still up there, good. If I click on the notification, it takes me back to the activity that created it, great. If I click on a button to cancel the notification, I get a NullPointerException.

Here's the call:

if (notificationManager == null)
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Intent intent = new Intent(this, myClass.class);
Notification notification = new Notification(android.R.drawable.icon, getString(R.string.notify), System.currentTimeMillis());
notification.setLatestEventInfo(myClass.this, "AppName","Description", PendingIntent.getActivity(this.getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));

notificationManager.notify(1, notification);

Here's the cancel:

private OnClickListener onClick_Buttons = new OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
            case R.id.btn:
                notificationManager.cancel(1);

                break;
            }
        } catch (Exception e) {
            Log.e(getString(R.string.app_name), e.getMessage());
        }
    }
};

Here's the logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
    at android.util.Log.println_native(Native Method)
    at android.util.Log.e(Log.java:215)
    at com.myApp.myClass$2.onClick(myClass.java:281)
    at android.view.View.performClick(View.java:2408)
    at android.view.View$PerformClick.run(View.java:8817)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

What is it that I am missing?

Thanks

8
  • Whats the stack trace in logcat? Commented Jul 14, 2011 at 2:30
  • I've added it to the question. Commented Jul 14, 2011 at 2:39
  • Whats at ZygoteInit.java line 868? Commented Jul 14, 2011 at 3:01
  • I have no clue. com.android.internal.os.... There's only 364 lines in my activity. Commented Jul 14, 2011 at 3:05
  • 1
    Actually, I figured it out, but I'll give you credit :). Answer with: put this `if (notificationManager == null) notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);' before the cancel line. Commented Jul 14, 2011 at 3:31

1 Answer 1

4

A little metalworker told me: put this `

if (notificationManager == null)
  notificationManager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);

before the cancel line

Sign up to request clarification or add additional context in comments.

1 Comment

So nice for it to be recognized! :) Hope the rest of the application goes well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.