I have the following login form:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".login">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginTop="24dp"
android:src="@drawable/logoradiosingular"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="50dp" />
<EditText
android:id="@+id/txtUsuario"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="175dp"
android:layout_marginEnd="93dp"
android:layout_marginRight="93dp"
android:background="#ccc"
android:drawableLeft="@drawable/icon_user"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Usuário"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="214dp" />
<EditText
android:id="@+id/pwsSenha"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignEnd="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginEnd="49dp"
android:layout_marginBottom="219dp"
android:background="#ccc"
android:drawableLeft="@drawable/icon_password"
android:ems="10"
android:hint="Senha"
android:inputType="textPassword"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="286dp" />
<Button
android:id="@+id/btnLogar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="148dp"
android:layout_marginRight="148dp"
android:layout_marginBottom="143dp"
android:text="Login"
android:onClick="logar"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="369dp" />
<Button
android:id="@+id/btnCadastrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="22dp"
android:layout_marginBottom="20dp"
android:text="Novo Cadastro"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="458dp" />
</RelativeLayout>
JAVA
package carcleo.com.radiosingular;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class login extends Activity {
private EditText usuario;
private EditText senha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usuario = (EditText)findViewById(R.id.txtUsuario);
senha = (EditText)findViewById(R.id.txtSenha);
}
public void logar (View v) {
AlertDialog.Builder dlg2 = new AlertDialog.Builder(this);
dlg2.setTitle("Aviso");
dlg2.setMessage("Ok até aqui!");
if(usuario.getText().toString() == null || usuario.getText().toString().equals("")){
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage("Usuário não pode estar vazio!");
usuario.setFocusable(true);
return;
}
if(senha.getText().toString() == null || senha.getText().toString().equals("")){
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage("Senha não pode estar vazia!");
senha.setFocusable(true);
return;
}
if (usuario.getText().toString() != "admi") {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage("Usuário Errado!");
usuario.setFocusable(true);
return;
}
if (senha.getText().toString() != "senha") {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage("Senha Errada!");
senha.setFocusable(true);
return;
}
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage("Tudo Certo");
Intent intent = new Intent(this, Principal.class);
startActivity(intent);
}
}
Well, I am testing directly on Moto G 4 Play
because my machine can not handle an emulator very well.
The problem is that when I click on botão de logar
, the program stops and gives the warning on the screen:
Radio Singular parou
Abrir app novamente?
Where am I going wrong?