0

当我尝试在片段类中为 Notificatoin Manager 分配值时,我有空指针异常。

    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
4

2 回答 2

7

试试这个方法

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

代替

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

它工作正常Frgament

于 2014-05-15T12:42:46.353 回答
0

对于Kotlin 中 Fragment 中的通知管理器

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

您需要在onCreateView中添加此代码

就这样!

于 2020-12-15T06:51:52.530 回答