我注意到使用快捷方式Alt + Insert
并选择构建器,它会尝试使用每个私有属性(例如cManager
和mTextView
)创建构造函数。我记得我已经创建了没有它并且工作正常的项目。
运行应用程序时,在完整的logcat和Activity
构造函数之后发生错误:
具有私有属性的构造函数:
private ConnectivityManager cManager;
private TextView mTextView;
public SplashScreenActivity() {
this.cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
this.mTextView = (TextView)this.findViewById(R.id.mProgressText);
}
logcat的:
02-20 18:20:56.155 1591-1591/com.universo91.towersrock I/art﹕ Not late-enabling -Xcheck:jni (already on)
02-20 18:20:58.355 1591-1591/com.universo91.towersrock D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
02-20 18:20:58.361 1591-1591/com.universo91.towersrock E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.universo91.towersrock, PID: 1591
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.universo91.towersrock/com.universo91.towersrock.Pages.SplashScreenActivity}: java.lang.InstantiationException: class com.universo91.towersrock.Pages.SplashScreenActivity has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.InstantiationException: class com.universo91.towersrock.Pages.SplashScreenActivity has no zero argument constructor
at java.lang.Class.newInstance(Class.java:1563)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at java.lang.Class.newInstance(Class.java:1561)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-20 18:20:58.772 1591-1603/com.universo91.towersrock I/art﹕ Background sticky concurrent mark sweep GC freed 1747(114KB) AllocSpace objects, 0(0B) LOS objects, 37% free, 391KB/623KB, paused 825us total 382.655ms
02-20 18:21:29.824 1690-1690/com.universo91.towersrock D/AndroidRuntime﹕ Shutting down VM
02-20 18:21:29.829 1690-1690/com.universo91.towersrock E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.universo91.towersrock, PID: 1690
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.universo91.towersrock/com.universo91.towersrock.Pages.SplashScreenActivity}: java.lang.InstantiationException: class com.universo91.towersrock.Pages.SplashScreenActivity has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.InstantiationException: class com.universo91.towersrock.Pages.SplashScreenActivity has no zero argument constructor
at java.lang.Class.newInstance(Class.java:1563)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at java.lang.Class.newInstance(Class.java:1561)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-20 18:21:30.168 1690-1702/com.universo91.towersrock I/art﹕ Background sticky concurrent mark sweep GC freed 1749(114KB) AllocSpace objects, 0(0B) LOS objects, 37% free, 391KB/623KB, paused 856us total 166.355ms
02-20 18:21:53.341 1690-1690/com.universo91.towersrock I/Process﹕ Sending signal. PID: 1690 SIG: 9
02-20 18:26:00.878 1754-1754/com.universo91.towersrock D/AndroidRuntime﹕ Shutting down VM
02-20 18:26:00.903 1754-1754/com.universo91.towersrock E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.universo91.towersrock, PID: 1754
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.universo91.towersrock/com.universo91.towersrock.Pages.SplashScreenActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4989)
at com.universo91.towersrock.Pages.SplashScreenActivity.<init>(SplashScreenActivity.java:24)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-20 18:26:01.144 1754-1766/com.universo91.towersrock I/art﹕ Background sticky concurrent mark sweep GC freed 1745(118KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 382KB/623KB, paused 950us total 131.218ms
02-20 18:26:04.429 1754-1754/com.universo91.towersrock I/Process﹕ Sending signal. PID: 1754 SIG: 9
答案 0 :(得分:2)
所有初始化都应使用onCreate()
的{{1}}方法执行:
Activity
覆盖@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
this.mTextView = (TextView)this.findViewById(R.id.mProgressText);
}
的构造函数涉及相当多的繁重,并且真的不是在公园散步。虽然你可以 [当然]有一个Activity
的空构造函数,但在Android框架的上下文中它确实是多余的。
相关答案:
1。 Why I cannot pass parameters to Android Activity Constructor。
答案 1 :(得分:2)
Android活动可以拥有构造函数,只要它们采用零参数即可。事实上,只要在声明它时初始化任何字段,就会有这样的构造函数。
但正如您的日志所说/Users/User/npm-global/bin/lessc -> /Users/User/npm-global/lib/node_modules/less/bin/lessc
less@2.7.1 /Users/User/npm-global/lib/node_modules/less
├── graceful-fs@4.1.6
├── mime@1.3.4
├── image-size@0.5.0
├── source-map@0.5.6
├── errno@0.1.4 (prr@0.0.0)
├── mkdirp@0.5.1 (minimist@0.0.8)
└── promise@7.1.1 (asap@2.0.4)
,所以您无法从构造函数中调用System services not available to Activities before onCreate()
。
所以https://stackoverflow.com/a/28636652/53974中的代码是正确的,但原因不是。
答案 2 :(得分:1)
您的代码应该在onCreate()
中不要为构建活动而烦恼。
答案 3 :(得分:0)
Android活动仅允许使用类默认构造函数。即参数为零的构造函数。在默认构造函数中,您可以对那些最终变量进行初始化,只有在生命周期onCreate方法之后才可用的链接系统服务除外。