“Attempt to Invoke Virtual Method 'android.view.Window$Callback android.view.Window.getCallback()' on a Null Object Reference” in Android
This error occurs when an Activity attempts to access views before it has been fully initialized. Specifically, the error is triggered when the Window.getCallback() method is called on a null object, which can happen if the setContentView() method has not yet been invoked in onCreate().
Cause:
To prevent this error, it is important to declare view fields without initializing them in the class declaration:
private EditText usernameField, passwordField; private TextView error; private ProgressBar progress;
Then, assign values to these fields within onCreate() after setContentView() has been called:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameField = (EditText)findViewById(R.id.username); passwordField = (EditText)findViewById(R.id.password); error = (TextView)findViewById(R.id.error); progress = (ProgressBar)findViewById(R.id.progress); }
Additional Advice:
new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, LoginActivity.class); startActivity(intent); finish(); } }, 1500);
The above is the detailed content of Why Do I Get 'Attempt to Invoke Virtual Method 'android.view.Window$Callback android.view.Window.getCallback()' on a Null Object Reference' in Android?. For more information, please follow other related articles on the PHP Chinese website!