向后移植应用程序时,ConnectivityManager NullPointerException

时间:2019-06-14 16:15:57

标签: java android java-7 backport

我正在将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有关的问题吗? 不确定在这里如何进一步前进

谢谢!

0 个答案:

没有答案