An Activity
is not fully initialized and ready to look up views until after setContentView(...)
is called in onCreate()
.
Only declare the fields like the following:
private EditText usernameField, passwordField;
private TextView error;
private ProgressBar progress;
and then assign the values in onCreate
:
@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);
}
Might not be part of the problem but as an extra bit of advice a Timer
runs the TimerTask
on a background thread and that should be avoided in this case. Replace the Timer
with a Handler
instead to run it on the UI thread.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, 1500);
Views defined in xml file can only be accessed in java code, after setting the ContentView
as the required xml file using:
setContentView(R.layout.xml_file_name);
So 1st call above method inside onCreate
method and then initialize the View
instances inside onCreate
or inside the methods in which the instance will be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With