我是Android编程的新手,我刚开始学习,我正在逐步学习,以及使用Java所拥有的知识。
我想创建一个notification
类,以便在我想要发出通知的任何时候保留所有通知方法。出于某种原因,我正在使用异常,而程序正在创建NotificationManager
对象以生成通知通道。只有在将notification
类作为单独的类时才会出现这种情况。
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
类中时,它的效果非常好。如果我将notification
类设为MainActivity
的内部类,它甚至可以工作。我只是不明白为什么当我将它复制到一个单独的类来创建一个对象时会有异常。
请注意,这基于API 26+或Android 8.0.0
__
P.S。我希望我的问题足够具体。这是我第一次在这里问一个问题所以请告诉我是否有需要进一步澄清的事情。感谢。
答案 0 :(得分:1)
您正尝试在context
初始化之前创建通知渠道。在使用之前初始化上下文,如下所示:
public notifications(Context c) {
context = c;
createNotificationChannel();
}