I get no errors but when I run the app it crashes and I don't know why! Here is the code, I am sure the error is here because it was working until here. I've been trying to search for the problem for about 4 hours and still have no clue!
public class MainActivity extends Activity implements OnClickListener {
EditText et1, et2, usuario1, password1, password2;
Button ingresar, borrar, ajustes, cancelarBtn, okusBtn;
Dialog dialog;
Context context = this;
String usuario = "";
String password = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
ingresar=(Button)findViewById(R.id.ingresar);
borrar=(Button)findViewById(R.id.borrar);
ajustes=(Button)findViewById(R.id.ajustesBtn);
cancelarBtn =(Button)dialog.findViewById(R.id.cancelarUs);
okusBtn =(Button)dialog.findViewById(R.id.okUs);
password1=(EditText)dialog.findViewById(R.id.password1);
password2=(EditText)dialog.findViewById(R.id.password2);
usuario1=(EditText)dialog.findViewById(R.id.usuario1);
borrar.setOnClickListener(this);
ingresar.setOnClickListener(this);
ajustes.setOnClickListener(this);
cancelarBtn.setOnClickListener(this);
okusBtn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.ingresar:
String usRecibido=et1.getText().toString();
String pasRecibido=et2.getText().toString();
if(usRecibido.equals(usuario) && pasRecibido.equals(password)){
Intent pancar = new Intent("com.example.iniciosesion.Iniciado");
Bundle bolsa = new Bundle();
bolsa.putString("NombreKey", usRecibido);
pancar.putExtras(bolsa);
startActivity(pancar);
}else{
Toast ts=Toast.makeText(this, "Usuario o contraseƱa incorrecto", Toast.LENGTH_LONG);
ts.show();
}
break;
case R.id.borrar:
et1.setText("");
et2.setText("");
break;
case R.id.ajustesBtn:
dialog = new Dialog(context);
dialog.setContentView(R.layout.controlusuario);
dialog.setTitle("Modificar usuario");
if(usuario == "" && password == ""){
password1.setHint("Crear password");
}else{
password1.setHint("Password actual");
}
break;
case R.id.cancelarUs:
dialog.dismiss();
break;
case R.id.okUs:
if(usuario == "" && password == "" && password1.getText().toString() == password2.getText().toString()){
usuario = usuario1.getText().toString();
password = password1.getText().toString();
}
if(usuario1.equals(usuario) && password1.equals(password))
password = password2.getText().toString();
usuario = usuario1.getText().toString();
break;
}
}
}
Try this code I have initialized the dialog in the
onCreate()
method