Iam trying to make the Admin Login but my login activity keep giving me error, there is 2 ways to login for admin they would click on another textview to change some settings then login...login works fine for users, but to admin it keeps giving me errors..
my login Activity:
private TextView register;
private Button login;
private TextInputLayout phoneNum, inputPassword;
private ProgressDialog loadingDialog;
private TextView adminLink, notAdminLink, title;
private String parentDbName = "Users";
private CheckBox rememberMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
loadingDialog = new ProgressDialog(this);
title = findViewById(R.id.title);
//Button
register = findViewById(R.id.register);
login = findViewById(R.id.loginBtn);
//EditText
phoneNum = findViewById(R.id.login_phone_num);
inputPassword = findViewById(R.id.login_passBox);
//Checkbox
rememberMe = findViewById(R.id.remember_me_chk);
//admin
adminLink = findViewById(R.id.admin_link);
notAdminLink = findViewById(R.id.not_admin);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
finish();
}
});
//CheckBox EVERY time app start
SharedPreferences preferences = getSharedPreferences("checkBox", MODE_PRIVATE);
String checkBox = preferences.getString("remember", "");
if (checkBox.equals("true")) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else if (checkBox.equals("false")) {
Toast.makeText(this, "Please Sign In.", Toast.LENGTH_SHORT).show();
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginUser();
}
});
// Automatically Login
rememberMe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
SharedPreferences preferences = getSharedPreferences("checkBox", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("remember", "true");
editor.apply();
} else if (!compoundButton.isChecked()){
SharedPreferences preferences = getSharedPreferences("checkBox", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("remember", "false");
editor.apply();
}
}
});
//Admins
adminLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login.setText(R.string.Admin_login);
title.setText(R.string.admin_panel_title);
rememberMe.setVisibility(View.INVISIBLE);
adminLink.setVisibility(View.INVISIBLE);
notAdminLink.setVisibility(View.VISIBLE);
parentDbName = "Admins";
}
});
notAdminLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login.setText(R.string.login);
title.setText(R.string.login);
rememberMe.setVisibility(View.VISIBLE);
adminLink.setVisibility(View.VISIBLE);
notAdminLink.setVisibility(View.INVISIBLE);
parentDbName = "Users";
}
});
}
private void LoginUser() {
String phone = phoneNum.getEditText().getText().toString();
String pass = inputPassword.getEditText().getText().toString();
if (TextUtils.isEmpty(phone)) {
Toast.makeText(this, "Please Enter Your PhoneNum.", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(this, "Please Enter Your Password.", Toast.LENGTH_SHORT).show();
} else {
loadingDialog.setTitle("Login Your Account...");
loadingDialog.setMessage("Connecting...");
// prevent it from disappearing when clicked outside
loadingDialog.setCanceledOnTouchOutside(false);
loadingDialog.show();
AccessAccount(phone,pass);
}
}
private void AccessAccount(String phone, String pass) {
final DatabaseReference rootRef;
rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(parentDbName).child(phone).exists()) {
Users userData = snapshot.child(parentDbName).child(phone).getValue(Users.class);
if (userData.getPhone().equals(phone)) {
if (userData.getPassword().equals(pass)) {
if (parentDbName.equals("Admins")) {
Toast.makeText(LoginActivity.this, "Admin: " userData.getName() "\nWish You have a good Day", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
startActivity(new Intent(LoginActivity.this, AdminCategoryActivity.class));
finish();
} else if (parentDbName.equals("Users")) {
Toast.makeText(LoginActivity.this, "Welcome Back " userData.getName() " Wish You have a good Day", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
} else {
Toast.makeText(LoginActivity.this, "Password is Incorrect.", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
}
}
} else {
Toast.makeText(LoginActivity.this, "This phone Number doesn't Exist.", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
Error Message :
CodePudding user response:
*Users userData = snapshot.child(parentDbName).child(phone).getValue(Users.class);
*In this line you are passing textInputLayout as child that's the error
*Check the child name in your firebase database
CodePudding user response:
You are storing a field as long in Firebase and Parsing that Field as String in app side.Most Probably that field is phone