System services not available to Activities before onCreate()

09-08 20:59:31.601: W/dalvikvm(22287): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-08 20:59:31.609: E/AndroidRuntime(22287): FATAL EXCEPTION: main
09-08 20:59:31.609: E/AndroidRuntime(22287): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.leranxinsheng.activity/com.leranxinsheng.activity.BottomMenuActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.os.Looper.loop(Looper.java:130)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-08 20:59:31.609: E/AndroidRuntime(22287): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 20:59:31.609: E/AndroidRuntime(22287): at java.lang.reflect.Method.invoke(Method.java:507)
09-08 20:59:31.609: E/AndroidRuntime(22287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-08 20:59:31.609: E/AndroidRuntime(22287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-08 20:59:31.609: E/AndroidRuntime(22287): at dalvik.system.NativeStart.main(Native Method)
09-08 20:59:31.609: E/AndroidRuntime(22287): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.Activity.getSystemService(Activity.java:3536)
09-08 20:59:31.609: E/AndroidRuntime(22287): at com.leranxinsheng.net.ThreadApHelper.<init>(ThreadApHelper.java:48)
09-08 20:59:31.609: E/AndroidRuntime(22287): at com.leranxinsheng.activity.BottomMenuActivity.<init>(BottomMenuActivity.java:47)
09-08 20:59:31.609: E/AndroidRuntime(22287): at java.lang.Class.newInstanceImpl(Native Method)
09-08 20:59:31.609: E/AndroidRuntime(22287): at java.lang.Class.newInstance(Class.java:1409)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-08 20:59:31.609: E/AndroidRuntime(22287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
09-08 20:59:31.609: E/AndroidRuntime(22287): ... 11 more

 

 

这是出现的错误信息。

原因是:Activity中的一个类变量的构造函数需要激活系统服务。而这个类变量的实例化是直接在Activity类里面,而不是在OnCreate函数里面。

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

你可能感兴趣的:(available)