这段代码抛出java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Context.getSystemServiceName(java.lang.Class)”
MainActivity.java
public class MainActivity extends AppCompatActivity {
...
protected void main() {
ArrayList<Channel> channelArrayList = channels();
for (int i = 0; i < channelArrayList.size(); i++) {
try {
Log.i("Channel", "Attempting to create channel: '" + channelArrayList.get(i).getChannel_id() + "'");
this.createChannel(channelArrayList.get(i));
} catch (Exception exception) {
Log.e("Channel", "Attempt to create channel '" + channelArrayList.get(i).getChannel_id() + "' failed - Exception " + exception);
}
}
}
...
public void createChannel(Channel channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
channel.getChannel_id(),
channel.getChannel_name(),
channel.getChannel_importance()
);
notificationChannel.setDescription(channel.getChannel_description());
NotificationManager notificationManager;
notificationManager = getSystemService(NotificationManager.class); //error occurs here
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
请注意,主要 Activity 实际上并不是您所期望的主要 Activity 。它只是在实际主 Activity (称为 LauncherActivity.java)中调用的一个类,如下所示:
LauncherActivity.java
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
MainActivity mainActivity = new MainActivity();
mainActivity.main();
}
完整异常消息
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.app, PID: 16793
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.app/com.company.app.LauncherActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getSystemServiceName(java.lang.Class)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getSystemServiceName(java.lang.Class)' on a null object reference
at android.content.ContextWrapper.getSystemServiceName(ContextWrapper.java:713)
at android.content.Context.getSystemService(Context.java:3156)
at com.company.app.MainActivity.createChannel(MainActivity.java:91)
at com.company.app.MainActivity.main(MainActivity.java:74)
at com.company.app.LauncherActivity.onCreate(LauncherActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
知道导致此问题的原因以及如何解决它吗?
最佳答案
您永远不应该直接实例化 Activity !
MainActivity mainActivity = new MainActivity();
像这样,基本上下文未附加,因此为空。
如果您想重用 main()
方法,请将其设为静态并提供 Context
作为参数。
protected static void main(Context context) {
ArrayList<Channel> channelArrayList = channels();
for (int i = 0; i < channelArrayList.size(); i++) {
try {
Log.i("Channel", "Attempting to create channel: '" + channelArrayList.get(i).getChannel_id() + "'");
createChannel(context, channelArrayList.get(i));
} catch (Exception exception) {
Log.e("Channel", "Attempt to create channel '" + channelArrayList.get(i).getChannel_id() + "' failed - Exception " + exception);
}
}
}
private static void createChannel(Context context, Channel channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
channel.getChannel_id(),
channel.getChannel_name(),
channel.getChannel_importance());
notificationChannel.setDescription(channel.getChannel_description());
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}
关于java - getSystemServiceName(...) 调用时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918419/