无法以管理员身份登录

时间:2020-03-18 10:37:17

标签: android string firebase firebase-realtime-database long-integer

在我的应用中,我可以以用户或管理员身份登录。我可以以用户身份成功登录,但是尝试以管理员身份登录我的应用程序时遇到错误。即使输入正确的凭据,我也被重定向到Launcheractivity。错误提示无法将Long转换为String,但是我在任何地方都没有使用Long。 Database Structure

登录活动

<employees [employees]="employees"></employees>

Logcat中显示的错误

public class loginActivity extends AppCompatActivity {
    private EditText InputPhoneNumber, InputPassword;
    private Button LoginButton;
    private ProgressDialog loadingBar;
    private TextView AdminLink, NotAdminLink;



    private String parentDbName = "Users";

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

        LoginButton = (Button) findViewById(R.id.Login_btn);
        InputPassword = (EditText) findViewById(R.id.login_Password_input);
        InputPhoneNumber = (EditText) findViewById(R.id.login_phone_number_input);
        AdminLink = (TextView)  findViewById(R.id.admin_panel_link);
        NotAdminLink = (TextView)  findViewById(R.id.not_admin_panel_link);
        loadingBar = new ProgressDialog(this);

        Paper.init(this);

        LoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                LoginUser();
            }
        });

        AdminLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                 LoginButton.setText("Login Admin");
                 AdminLink.setVisibility(View.INVISIBLE);
                 NotAdminLink.setVisibility(View.VISIBLE);
                 parentDbName = "Admins";


            }
        });

                NotAdminLink.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view)
                    {
                        LoginButton.setText("Login");
                        AdminLink.setVisibility(View.VISIBLE);
                        NotAdminLink.setVisibility(View.INVISIBLE);
                        parentDbName = "Users";
                    }
                });


    }


    private void LoginUser()
    {
        String phone = InputPhoneNumber.getText().toString();
        String password = InputPassword.getText().toString();

          if(TextUtils.isEmpty(phone))
            {
                Toast.makeText(this, "Please write your phone...", Toast.LENGTH_SHORT).show();
            }
                 else if(TextUtils.isEmpty(password))
            {
                Toast.makeText(this, "Please write your password...", Toast.LENGTH_SHORT).show();
            }
            else
          {
              loadingBar.setTitle("Login Account");
              loadingBar.setMessage("Please wait, while we are Checking the Credential");
              loadingBar.setCanceledOnTouchOutside(false);
              loadingBar.show();

              AllowAccessAccount(phone, password);
          }
    }

        private void AllowAccessAccount(final String phone, final String password)
        { final DatabaseReference RootRef;
            RootRef = FirebaseDatabase.getInstance().getReference();

            RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                {
                       if(dataSnapshot.child(parentDbName).child(phone).exists())
                       {
                           Users usersData = dataSnapshot.child(parentDbName).child(phone).getValue(Users.class);
                           if (usersData != null)
                           {
                               if (usersData.getPhone().equals(phone))
                               {
                                   if (usersData.getPassword().equals(password))
                                   {

                                       if (parentDbName.equals("Admins"))
                                       {
                                           Toast.makeText(loginActivity.this, "Welcome Admin, you are Logged in Successfully..", Toast.LENGTH_SHORT).show();
                                           loadingBar.dismiss();

                                           Intent intent = new Intent(loginActivity.this, AdminCategoryActivity.class);
                                           startActivity(intent);
                                       }
                                       else if (parentDbName.equals("Users"))
                                       {
                                           Toast.makeText(loginActivity.this, "Logged in Successfully..", Toast.LENGTH_SHORT).show();
                                           loadingBar.dismiss();

                                           Intent intent = new Intent(loginActivity.this, HomeActivity.class);
                                           Prevalent.currentOnlineUser = usersData;
                                           startActivity(intent);
                                       }
                                   }
                               }
                               else
                               {
                                   Toast.makeText(loginActivity.this, "Password is Incorrect..", Toast.LENGTH_SHORT).show();
                                   loadingBar.dismiss();
                               }
                           }


                       }
                       else
                       {
                           Toast.makeText(loginActivity.this,"Account with This" + phone + " number do not exist", Toast.LENGTH_SHORT).show();
                           loadingBar.dismiss();
                       }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError)
                {

                }
            });
        }
}

用户(模型类)

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ecommerce1, PID: 4921
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String at com.example.ecommerce1.loginActivity$4.onDataChange(loginActivity.java:123)

1 个答案:

答案 0 :(得分:0)

数据库中的

phone类型为Long,而Users POJO类中的电话类型为String,则需要将POJO类中的类型更改为{{1} }:

Long

另一种解决方案是将其保留为Long phone ,但是在保存时,可以使用pojo类保存到数据库,例如:

String