Reputation: 11
This is the simple java code used by me on login screen. App closes itself after the splash screen which I applied. It doesnt go any further. what to do?
public class MainActivity extends AppCompatActivity {
EditText username = (EditText)findViewById(R.id.input_email);
EditText password = (EditText)findViewById(R.id.input_password);
Button loginButton = (Button) findViewById(R.id.btn_login);
int counter=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
//Login Button Code
public void onBtn(View v){
if(username.getText().toString().equals("[email protected]")&&
(password.getText().toString().equals("shubham")))
{
Toast.makeText(this, "Login Success", Toast.LENGTH_LONG).show();}
else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show();
counter--;
if(counter==0){ loginButton.setEnabled(false); }
else
{
Toast.makeText(this, counter+" attempts left", Toast.LENGTH_SHORT).show(); }
}}
//Sign Up Button Code
public void linkSign(View v){
Intent intent = new Intent(this, signupActivity.class);
startActivity(intent);
}}
Upvotes: 1
Views: 2031
Reputation: 781
You initialize EditText
and Button
view on wrong place ,you need to initialize it inside onCreate
method like below.
public class MyActivity extends AppCompatActivity {
EditText username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.input_email);
...
}
}
Upvotes: 0
Reputation: 37404
move
EditText username = (EditText)findViewById(R.id.input_email);
EditText password = (EditText)findViewById(R.id.input_password);
Button loginButton = (Button) findViewById(R.id.btn_login);
after setContentView
while keeping reference variable outside oncreate
because views are only available in the UI hierarchy of the activity after invocation of setContentView
EditText username;
EditText password;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.input_email);
password = (EditText)findViewById(R.id.input_password);
loginButton = (Button) findViewById(R.id.btn_login);
}
Upvotes: 2