This question is a development of the Java + Android theme : application crash when using findViewById . From the answer to this question, I learned that the findViewById method cannot be used before the onCreate method, and, in terms of the code sequence, and outside the onCreate method (I quote the answer):

The findViewById method cannot be called before the onCreate method, and it is meaningless until setContentView. And you use it to initialize the field, that is, it is called even before the constructor ... At this point, nothing has been initialized yet in the activation, she does not know anything about any views.

Here is my error code from that question:

 public class AddItem extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_item); } LinearLayout AddItemContainer = (LinearLayout) findViewById(R.id.AddItemContainer); } 

However, in the Android Studio template, the findViewById method is found dyt onCreate in the onBackPressed() and onNavigationItemSelected(MenuItem item) methods. Why it does not cause an error?

 package jp.co.wajyouhougijyutsu.yd.myapplication2; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } 

    3 answers 3

    The order of the method call during the activation initialization is

    1. Static variables are initialized and static initialization blocks are executed.
    2. The constructor is executed (for activations and fragments, it is rigidly tied to the system - never touch it)
    3. Running onCreate method
    4. Inside onCreate (), the implementation of this method is called in the superclass.
    5. Only now you can call setContentView and load the markup.
    6. Now you can refer to the markup - it is loaded. All subsequent life cycle methods also have access to it.

    At the same time, your own self-styled methods follow the same rule - if you create a method in which you will access the markup, it will correctly work out when you call it on onCreate after setContentView but will cause an error if you call it up to this point

      Because the methods you listed are called after onCreate(...) called.

        If the user calls onBackPressed() or onNavigationItemSelected(MenuItem item) with its actions, this will not cause an error, because at that moment onCreate work and the markup will be drawn.