Fragment中的通知管理器

时间:2014-05-15 12:41:58

标签: java android nullpointerexception notificationmanager

当我尝试在片段类中为Notification Manager分配值时,我有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");
}

错误日志

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

2 个答案:

答案 0 :(得分:6)

试试这种方式

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

而不是

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

它在Frgament中正常工作。

答案 1 :(得分:0)

对于科特林片段中的通知管理器

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

您需要在 onCreateView

中添加此代码

仅此而已!