0

When i try to assign value for Notificatoin Manager in fragment class, i have Null Pointer Exception.

    private NotificationManager m_notificationMgr;

public void CreateNotification() {
    Log.d(TAG, "created");

    m_stopwatch = new Stopwatch();
    m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//null pointer exception

    createNotification();
    Log.d(TAG, "ok");
}

Error logs

05-15 12:31:16.127: E/AndroidRuntime(2216): FATAL EXCEPTION: main
05-15 12:31:16.127: E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timetracker/com.example.timetracker.MainActivity}: java.lang.NullPointerException
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.os.Looper.loop(Looper.java:137)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at java.lang.reflect.Method.invoke(Method.java:525)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at dalvik.system.NativeStart.main(Native Method)
05-15 12:31:16.127: E/AndroidRuntime(2216): Caused by: java.lang.NullPointerException
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.content.ContextWrapper.getSystemService(ContextWrapper.java:519)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at com.example.timetracker.StopwatchService.Create(StopwatchService.java:51)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at com.example.timetracker.MainActivity.onCreate(MainActivity.java:125)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.Activity.performCreate(Activity.java:5133)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 12:31:16.127: E/AndroidRuntime(2216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-15 12:31:16.127: E/AndroidRuntime(2216):     ... 11 more
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
kolodach
  • 997
  • 1
  • 8
  • 12

2 Answers2

8

try this way

 m_notificationMgr = (NotificationManager)getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);

instead of

 m_notificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

it's working fine in Frgament.

M D
  • 47,665
  • 9
  • 93
  • 114
  • It does no working. solved the problem this way: i create static copy of Notificationmanager in main class, and assign value of this copy in my fragment class. – kolodach May 15 '14 at 13:01
0

For the Notification Manager in Fragment in Kotlin,

val manager = activity!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)

You need to add this code in onCreateView

That's All!

Prateek
  • 502
  • 5
  • 16