1
votes

i am trying to login using data that are stored in firebase realtime database and when i try to login with phone number it gives an error and the ondatachange function not working.

public class LoginActivity extends AppCompatActivity {

    Button Log_in, sign_up;
    CountryCodePicker countryCodePicker;
    TextInputLayout number, password;

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

        Log_in = findViewById(R.id.Log_in);
        sign_up = findViewById(R.id.sign_up);
        number = findViewById(R.id.number);
        password = findViewById(R.id.password);
        countryCodePicker = findViewById(R.id.Login_ccp);
    }

    public void callHomrScreen(View view) {
        if (!validateNumber() | !validatePassword()){
            return;
        }
        //getting data
        String _phoneNumber = number.getEditText().getText().toString().trim();
        String _password = password.getEditText().getText().toString().trim();

       if (_phoneNumber.charAt(0)=='0'){
           _phoneNumber=_phoneNumber.substring(1);
       }

        String _completePhoneNumber = "+" + countryCodePicker.getFullNumber() + _phoneNumber;

        //???DatabaseQuery
        Query chekUser = FirebaseDatabase.getInstance().getReference("Users").orderByChild("phoneNumber").equalTo(_completePhoneNumber);

        chekUser.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()){
                    number.setError(null);
                    number.setErrorEnabled(false);

                    String systemPassword=snapshot.child(_completePhoneNumber).child("password").getValue(String.class);
                    if (systemPassword.equals(_password)){
                        password.setError(null);
                        password.setErrorEnabled(false);
                    }else{
                        Toast.makeText(LoginActivity.this, "Password Doesnot Exist!", Toast.LENGTH_SHORT).show();

                    }
                }else{
                    Toast.makeText(LoginActivity.this, "User Doesnot Exist!", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(LoginActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    public void callRegisterActivity(View view) {
        startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
    }


    private boolean validateNumber() {
        String val = number.getEditText().getText().toString().trim();

        if (val.isEmpty()) {
            number.setError("field cannot be empty");
            return false;
        } else {
            number.setError(null);
            number.setErrorEnabled(false);
            return true;
        }
    }

    private boolean validatePassword() {
        String val = password.getEditText().getText().toString().trim();
        String checkPassword = "^" +
            "(?=.*[0-9])" +         //at least 1 digit
            "(?=.*[a-z])" +         //at least 1 lower case letter
            "(?=.*[A-Z])" +         //at least 1 upper case letter
            "(?=.*[a-zA-Z])" +      //any letter
            "(?=.*[@#$%^&+=])" +    //at least 1 special character
            "(?=\\S+$)" +           //no white spaces
            ".{4,}" +               //at least 4 characters
            "$";

        if (val.isEmpty()) {
            password.setError("field cannot be empty");
            return false;
        }
        //else if (!val.matches(checkPassword)){
        //   password.setError("password should contain atleast 4 charecter");
        //   return false;
        //}
        else {
            password.setError(null);
            password.setErrorEnabled(false);
            return true;
        }
    }
}

Error E/CCP: getFullNumber: Could not parse number E/CCP: getFullNumber: Could not parse number E/OpenGLRenderer: GL error: 0x506 A/OpenGLRenderer: GL errors! frameworks/base/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp:126 A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 9153 (RenderThread), pid 9112

1
Hello and welcome to SO :) Please consider reducing your code to the absolute necessary and try to format it (so it's easier to read). Also a short summary of your problem, what you are trying to do, what you do and when the problem appears would be very helpful. Just posting code and an error message is not very attractive to read in the first place.GameDroids
@GameDroids ok doneBibek Timalsina
what you get response from countryCodePicker.getFullNumber() ?Jatin

1 Answers

1
votes

try to replace countryCodePicker.getFullNumber() with countryCodePicker.getSelectedCountryCode

so your phone number line is:

 String _completePhoneNumber = "+" + countryCodePicker.getSelectedCountryCode() + _phoneNumber;

using getSelectedCountryCode you get only number code like : 1,91, 81 and you get output like : +1xxxxxxxxxx