简体   繁体   中英

App stops working when I pressed the sign up button in Android Studio

Sign up button is working properly when the field is not empty. If the field is empty, the app keeps stopping. I don't know what's the reason and also it didn't verify the password and confirm password without confirmation it stored in the firebase. Can any one helpme. I have attached my java code and XML code in this Query.

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background">

<TextView
    android:id="@+id/app"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="150sp"
    android:layout_marginTop="60sp"
    android:layout_marginRight="100sp"
    android:layout_marginBottom="30sp"
    android:fontFamily="@font/font"
    android:text="Sign up"
    android:textColor="@color/textColor"
    android:textSize="50dp" />

<EditText
    android:id="@+id/email"
    android:letterSpacing="0.2"
    android:layout_below="@id/app"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="30sp"
    android:layout_marginLeft="30sp"
    android:inputType="textEmailAddress"
    android:fontFamily="@font/font"
    android:textColor="@color/textColor"
    android:hint="@string/mail"
    android:textColorHint="@color/textColor"
    android:drawableLeft="@drawable/ic_account"/>

<EditText
    android:id="@+id/password"
    android:letterSpacing="0.2"
    android:layout_below="@id/email"
    android:layout_marginTop="20sp"
    android:drawableLeft="@drawable/ic_lock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:layout_marginRight="30sp"
    android:layout_marginLeft="30sp"
    android:textColor="@color/textColor"
    android:hint="@string/password"
    android:fontFamily="@font/font"
    android:textColorHint="@color/textColor"/>


<EditText
    android:id="@+id/password1"
    android:layout_below="@id/password"
    android:layout_marginTop="20sp"
    android:letterSpacing="0.2"
    android:drawableLeft="@drawable/ic_lock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:layout_marginRight="30sp"
    android:layout_marginLeft="30sp"
    android:textColor="@color/textColor"
    android:hint="Confirm Password"
    android:fontFamily="@font/font"
    android:textColorHint="@color/textColor"/>

<Button
    android:id="@+id/btnLogin"
    android:layout_below="@id/password1"
    android:layout_width="100sp"
    android:layout_height="50sp"
    android:layout_marginLeft="160sp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30sp"
    android:background="@drawable/shapes"
    android:fontFamily="@font/font"
    android:text="Sign up"
    android:textColor="@color/textColor" />

<TextView
    android:id="@+id/errorTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/font"
    android:text=""
    android:textColor="#FF0000"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20sp"
    android:layout_marginLeft="160sp"/>


<TextView
    android:id="@+id/noaccount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnLogin"
    android:layout_gravity="center"
    android:layout_marginTop="30sp"
    android:layout_marginBottom="10sp"
    android:textSize="18sp"
    android:layout_marginLeft="275sp"
    android:fontFamily="@font/font"
    android:text="sign in"
    android:textColor="@color/signin" />

<TextView
    android:id="@+id/signin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnLogin"
    android:layout_gravity="center"
    android:layout_marginRight="85sp"
    android:layout_marginTop="30sp"
    android:layout_marginBottom="10sp"
    android:textSize="18sp"
    android:layout_marginLeft="85sp"
    android:fontFamily="@font/font"
    android:text="@string/alreadyAcc"
    android:textColor="@color/textColor" />

<TextView
    android:id="@+id/dash"
    android:layout_marginLeft="80sp"
    android:layout_below="@id/noaccount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dash"
    android:layout_gravity="center"
    android:layout_marginRight="80sp"
    android:layout_marginBottom="10sp"
    android:textColor="@color/textColor"
    android:fontFamily="@font/font"/>
<TextView
    android:id="@+id/dash1"
    android:layout_below="@id/dash"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signby"
    android:layout_marginRight="80sp"
    android:textSize="18dp"
    android:layout_marginLeft="150sp"
    android:layout_gravity="center"
    android:layout_marginBottom="10sp"
    android:textColor="@color/textColor"
    android:fontFamily="@font/font"/>

<ImageView
    android:layout_below="@id/dash1"
    android:layout_marginLeft="170sp"
    android:id="@+id/imageView"
    android:layout_marginBottom="50sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/telegram1" />

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_marginTop="25dp"
    android:outlineAmbientShadowColor="@color/textColor"
    android:layout_toRightOf="@id/btnLogin"
    android:layout_below="@id/password1"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

This is my Java Code. I'm a beginner. Please help me

package com.devanand.login;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import java.util.regex.Pattern;

public class Main2Activity extends AppCompatActivity {
EditText email, pass,cPass;
TextView textView,errorTxt;
Button btnlogin;
FirebaseAuth fAuth;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

textView =findViewById(R.id.noaccount);
email = findViewById(R.id.email);
pass = findViewById(R.id.password);
cPass = findViewById(R.id.password1);
btnlogin =findViewById(R.id.btnLogin);
errorTxt =findViewById(R.id.errorTxt);

fAuth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.progressBar);

if(fAuth.getCurrentUser()!= null) {
    Intent intent = new Intent(Main2Activity.this,after_login_main.class);
    finish();
}

    textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Main2Activity.this,MainActivity.class);
        startActivity(intent);
    }
});

    btnlogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String emailTxt = email.getText().toString();
        String passTxt = pass.getText().toString();
        String cPassTxt = cPass.getText().toString();

        if( validEmail(emailTxt) && validPassword(passTxt) && confirmPass(passTxt,cPassTxt))
        {
            resetError();
        }
        else
        {
            setError();
        }

        progressBar.setVisibility(View.VISIBLE);
        //register user in firebase.

        fAuth.createUserWithEmailAndPassword(emailTxt,passTxt).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {
                    Toast.makeText(Main2Activity.this,"Account created successfully",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Main2Activity.this,after_login_main.class); 
                    startActivity(intent);
                }else
                {
                    Toast.makeText(Main2Activity.this,"Try again",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
});

}

private boolean validEmail(String email) {
   return checkRegex("[\\w-]+@([\\w-]+\\.)+[\\w-]+",email);
}

private boolean validPassword(String pass) {
    return checkRegex("^(?=.*\\d).{4,8}$",pass);
}

private boolean confirmPass(String actualPass,String cPass)
{
    return actualPass.equals(cPass);
}

private void setError () {
    errorTxt.setVisibility(View.VISIBLE);
    errorTxt.setText("Please provide Valid details");
}

private void resetError () {
    errorTxt.setVisibility(View.INVISIBLE);
    errorTxt.setText("");
}


private boolean checkRegex(String pattern, String input) {
    Pattern r = Pattern.compile(pattern);
    return r.matcher(input).matches();
}

}

This is log cat of my Signup page:

2020-04-30 10:59:48.387 4048-4055/? E/art: Failed sending reply to debugger: Broken pipe
2020-04-30 11:00:06.456 4048-4048/com.devanand.login E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.devanand.login, PID: 4048
java.lang.IllegalArgumentException: Given String is empty or null
    at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(com.google.firebase:firebase-auth@@19.3.1:288)
    at com.devanand.login.Main2Activity$2.onClick(Main2Activity.java:100)
    at android.view.View.performClick(View.java:6203)
    at android.view.View$PerformClick.run(View.java:23239)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6261)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1064)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)

Wrap your code with the below if-loop that checks whether the fields are empty or not.

if (.emailTxt.equals("") &&.passTxt .equals("") && !cPassTxt .equals(""))

if (!emailTxt .equals("") && !passTxt .equals("") && !cPassTxt .equals(""))
{

     if( validEmail(emailTxt) && validPassword(passTxt) && confirmPass(passTxt,cPassTxt))
            {
                resetError();
            }
            else
            {
                setError();
            }
    }

else
{
//your-logic-to-handle in case of empty values
}

NOTE: Based on your requirements, you should use either the && or the || operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM