Исключение нулевого указателя уведомления Android - PullRequest
2 голосов
/ 14 июля 2011

У меня установлено оповещение в активности. Создает уведомление, как и ожидалось. Когда я возвращаюсь на главный экран, уведомление все еще там, хорошо. Если я нажму на уведомление, оно вернет меня к деятельности, которая его создала, отлично. Если я нажимаю кнопку, чтобы отменить уведомление, я получаю исключение NullPointerException.

Вот звонок:

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

Вот отмена:

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

Вот логкат:

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)

Что мне не хватает?

Спасибо

1 Ответ

4 голосов
/ 14 июля 2011

Маленький слесарь сказал мне: положить это `

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

до строки отмены

...