你的位置:首页 > 操作系统

[操作系统]System services not available to Activities before onCreate() you


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)