System services not available to Activities before onCreate()

我是研究僧i 提交于 2020-03-22 00:01:39

System services not available to Activities before onCreate()

在 MainActivity中定义如下变量并实例化导致。

LocationManager mgr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

此服务需要激活系统服务。,

解决方法:把此类变量的实例化工作放到MainActivity的OnCreate函数里面。

 

07-19 04:09:45.499: E/AndroidRuntime(5394): FATAL EXCEPTION: main
07-19 04:09:45.499: E/AndroidRuntime(5394): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{myApp.securitycore.com.comsecuritycoremyApp/myApp.securitycore.com.comsecuritycoremyApp.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.os.Looper.loop(Looper.java:137)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-19 04:09:45.499: E/AndroidRuntime(5394): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 04:09:45.499: E/AndroidRuntime(5394): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 04:09:45.499: E/AndroidRuntime(5394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 04:09:45.499: E/AndroidRuntime(5394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 04:09:45.499: E/AndroidRuntime(5394): at dalvik.system.NativeStart.main(Native Method)
07-19 04:09:45.499: E/AndroidRuntime(5394): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.Activity.getSystemService(Activity.java:4463)
07-19 04:09:45.499: E/AndroidRuntime(5394): at myApp.securitycore.com.comsecuritycoremyApp.MainActivity.<init>(MainActivity.java:75)
07-19 04:09:45.499: E/AndroidRuntime(5394): at java.lang.Class.newInstanceImpl(Native Method)
07-19 04:09:45.499: E/AndroidRuntime(5394): at java.lang.Class.newInstance(Class.java:1319)
07-19 04:09:45.499: E/AndroidRuntime(5394): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!