In a continuing activity class, cannot oncreate and constructor appear simultaneously ??
Reference: http://bbs.csdn.net/topics/390321638
If not, why?
If yes, why is an error?
Let's take a look at the Java example.
//. Javapublic Class A {public a () {system. out. println ("=== a constructor ====") ;}}/B. javapublic Class B {static A = new A (); static {system. out. println ("= B static =");} public B () {system. out. println ("B constructor");} {system. out. println ("= B Dynamic =") ;}// C. javapublic class c {/*** @ Param ARGs */public static void main (string [] ARGs) {try {class Aclass = Class. forname ("B"); object aninstance = Aclass. newinstance ();} catch (exception e) {e. printstacktrace ();} system. out. println ("============================"); B = new B ();}} /* print the result === a structure ========= B static ===== B Dynamic === B structure ========= B =================== B Dynamic === B construction */
Therefore, no parameter construction is called no matter whether new B () or class. newinstance () constructs B.
//. Javapublic Class A {public a () {system. out. println ("A construction without Parameters") ;}// B. javapublic Class B {public B () {system. out. println ("B No parameter construction") ;}// mainactivity. javapublic class mainactivity extends activity {static B = new B (); static {system. out. println ("static code block");} A = new A (); {system. out. println ("dynamic code block");} public mainactivity () {system. out. println ("activity Construction"); // context Ct = This; // CT. gettext (R. string. app_name) ;}@ overrideprotected void oncreate (bundle savedinstancestate) {system. out. println ("oncreate"); super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main );}}
Print results
B No parameter construction
Static code block
A no-parameter construction
Dynamic code block
Activity Construction
Oncreate
If it is changed to mainactivity ()
Will appear
Java. Lang. runtimeexception: Unable to instantiate activity componentinfo {com. example. Test/COM. example. Test. mainactivity}: Java. Lang. illegalaccessexception: Access to constructor not allowed
If it is changed
Public mainactivity (){
System. Out. println ("activity Construction ");
Context Ct = this;
Ct. gettext (R. String. app_name );
}
Will appear
Java. Lang. runtimeexception: Unable to instantiate activity componentinfo {com. example. Test/COM. example. Test. mainactivity}: Java. Lang. nullpointerexception
At com. example. Test. mainactivity. <init> (mainactivity. Java: 25)
At java. Lang. Class. newinstanceimpl (native method)
At java. Lang. Class. newinstance (class. Java: 1130)
We can also find that class. newinstance.
In fact, member variables cannot be initialized at the same time (?). This can be done only during writing, but it is processed during compilation.
<Clinit> the initialization method is as follows: 1. static member variable; 2. Static code block.
<Init> (How many constructor methods are there) Order 1. super () 2 initializes a common member variable. 3. Dynamic code block. 4. Code remaining except super () in the constructor. if the constructor has this (), other constructor is called. super () will be executed first in other structures that do not use this ()-in fact, this () will be replaced to get the result. note this (). super () can only be written in the first line. super exists. if no parameter is set for the parent class, this parameter can be left empty. but it is still executed. others must be written !!!
For example
public class G extends H{int a;int b;public G(int a, int b) {super(5);System.out.println("g(int,int)");this.a = a;this.b = b;}public G(int a){this(a,3);System.out.println("g(int)");}public G(){this(2);System.out.println("g()");}public static void main(String[] args) {G g=new G();}}
Output
G (INT, INT)
G (INT)
G ()
It can be seen that the android instantiated activity is implemented through class. newinstance. The called activity must be constructed without parameters and cannot be modified by private! It can be left blank by default. If there is a construction with parameters, there must also be a construction without parameters. because of this, you do not need to write this constructor unless the constructor is constructed. you can still use the dynamic code block for initialization without writing this parameter-free constructor. because of this, you do not need to explicitly write this parameter-free structure !!!
As we all know, an important bridge in activity is context.
If you write
Public mainactivity (){
System. Out. println ("activity Construction ");
Context Ct = this;
Ct. gettext (R. String. app_name );
}
An exception will occur. therefore, before oncreate, only context-independent objects can be initialized. Context-related objects should not be placed before oncreate. therefore, since context-related methods cannot be called, it makes no sense to initialize them before oncreate. write the init method to be initialized and put it in oncreate. convenient and convenient.