我正在将 Android 应用程序从 Android 9 反向移植到 Android 5.1
现在我明白了,我得到了错误
Process: com.name.systemhealthmonitor, PID: 3369
java.lang.RuntimeException: Unable to create service com.name.systemhealthmonitor.services.RealtimeMonitorService: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.ConnectivityManager.registerNetworkCallback(android.net.NetworkRequest, android.net.ConnectivityManager$NetworkCallback)' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2771)
at android.app.ActivityThread.access$1800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.ConnectivityManager.registerNetworkCallback(android.net.NetworkRequest, android.net.ConnectivityManager$NetworkCallback)' on a null object reference
at com.name.systemhealthmonitor.NetworkMonitor.start(NetworkMonitor.java:67)
at com.name.systemhealthmonitor.services.RealtimeMonitorService.onCreate(RealtimeMonitorService.java:72)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
造成这种情况的代码是
public void start() {
mStartTime = System.currentTimeMillis();
mCm.registerNetworkCallback(new NetworkRequest.Builder().build(), mNetworkCallback);
}
这是与使用的较低 API 版本或缺少 Java 8 有关的问题吗?不知道如何在这里进一步发展
谢谢!