参见英文答案 > What is a NullPointerException, and how do I fix it? 12个
我是AndroID编程的新手,我刚开始学习,我正在逐步学习,以及使用Java所拥有的知识.
我想创建一个通知类,以便在我想要发出通知的任何时候保留所有通知方法.出于某种原因,我在程序创建notificationmanager对象以创建通知通道时遇到异常.只有在将通知类作为单独的类时才会发生这种情况.
MainActivity类:
package com.example.colombian.textingclasses;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); notifications n = new notifications(this); }}
通知类:
import androID.app.NotificationChannel;import androID.app.notificationmanager;import androID.content.Context;import androID.os.Build;import androID.support.v7.app.AppCompatActivity;public class notifications { private Context context; public final String CHANNEL_ID = "customChannel"; public notifications(Context c) { createNotificationChannel(); context = c; } //creates a notification channel private voID createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "MyChannel"; String description = "Description"; int importance = notificationmanager.importANCE_HIGH; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description);/*******Error happens below*******/ notificationmanager nm = (notificationmanager) context.getSystemService(notificationmanager.class); //error nm.createNotificationChannel(channel); } }}
例外:
E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.colombian.textingclasses, PID: 16471 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.colombian.textingclasses/com.example.colombian.textingclasses.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object androID.content.Context.getSystemService(java.lang.class)' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2957) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) at androID.app.ActivityThread.-wrap11(UnkNown Source:0) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at androID.os.Handler.dispatchMessage(Handler.java:105) at androID.os.Looper.loop(Looper.java:164) at androID.app.ActivityThread.main(ActivityThread.java:6940) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object androID.content.Context.getSystemService(java.lang.class)' on a null object reference at com.example.colombian.textingclasses.notifications.createNotificationChannel(notifications.java:32) at com.example.colombian.textingclasses.notifications.<init>(notifications.java:19) at com.example.colombian.textingclasses.MainActivity.onCreate(MainActivity.java:14) at androID.app.Activity.performCreate(Activity.java:7174) at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2910) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) at androID.app.ActivityThread.-wrap11(UnkNown Source:0) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at androID.os.Handler.dispatchMessage(Handler.java:105) at androID.os.Looper.loop(Looper.java:164) at androID.app.ActivityThread.main(ActivityThread.java:6940) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
奇怪的是,当createNotificationChannel()方法在MainActivity类中时,它的工作完全正常.如果我将通知类作为MainActivity的内部类,它甚至似乎有效.我只是不明白为什么当我将它复制到一个单独的类来创建一个对象时有一个异常.
请记住,这基于API 26或AndroID 8.0.0
__
附:我希望我的问题足够具体.这是我第一次在这里问一个问题所以请告诉我是否有需要进一步澄清的事情.谢谢.
解决方法:
您正在尝试在初始化上下文之前创建通知通道.使用前初始化上下文如下:
public notifications(Context c) { context = c; createNotificationChannel(); }
总结 以上是内存溢出为你收集整理的java – Android:是否可以创建一个通知对象来调用? (而不是每次都写一个方法)全部内容,希望文章能够帮你解决java – Android:是否可以创建一个通知对象来调用? (而不是每次都写一个方法)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)