简体   繁体   English

dataSnapshot function 不工作 - 调用 function 时应用程序崩溃

[英]dataSnapshot function not working - App crashing when function call

I am trying to login using firebase realtime DB, where my username and password stored.我正在尝试使用 firebase 实时数据库登录,其中存储了我的用户名和密码。 when I enter the wrong username I'm getting the error.当我输入错误的用户名时,我得到了错误。 But when I enter correct credentials and hit login the app crashes.但是当我输入正确的凭据并点击登录时,应用程序崩溃了。 I don't know where I did the wrong, Please Help to solve this.不知道哪里做错了,求大神帮忙解决。 Thanksa lot in advance.非常感谢。

  1. Screenshot od firebase DB firebase DB截图

enter image description here在此处输入图像描述

  1. Android XML Android XML

     <ImageView android:id="@+id/login_img" android:layout_width="320dp" android:layout_height="186dp" android:layout_gravity="center" android:contentDescription="@string/login_img" android:src="@drawable/login" /> <TextView android:id="@+id/h_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello there, welcome back" android:textAllCaps="true" android:textColor="@color/colorPrimary" android:textSize="32sp" android:textStyle="bold" app:fontFamily="@font/roboto_bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login to continue" android:textColor="@color/colorPrimaryMid" android:textSize="24sp" android:layout_marginTop="5dp" android:layout_marginBottom="10dp"/> <com.google.android.material.textfield.TextInputLayout android:id="@+id/username" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:layout_marginBottom="10dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edtUsername" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/password" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" android:hint="Password" android:layout_marginBottom="10dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edtPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/forgot_pass" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginBottom="10dp" android:background="#00FFFFFF" android:elevation="0dp" android:freezesText="false" android:text="Forget Password?" /> <Button android:id="@+id/login_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:padding="10dp" android:text="Login" android:textColor="@color/colorPrimaryLight" android:onClick="loginUser"/> <Button android:id="@+id/reg_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00FFFFFF" android:padding="10dp" android:text="New user Register here." android:textColor="@color/colorSecondaryDark" android:onClick="regUser"/>
  2. login.java code login.java代码

package com.example.d1;

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 com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class login extends AppCompatActivity {

    TextInputLayout username, password;
    TextInputEditText edtUsername, edtPassword;
    Button login_btn, reg_btn;


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

        username = (TextInputLayout) findViewById(R.id.username);
        password = (TextInputLayout) findViewById(R.id.password);

        edtUsername = (TextInputEditText) findViewById(R.id.edtUsername);
        edtPassword = (TextInputEditText) findViewById(R.id.edtPassword);
    }

    private boolean validateUsername(){
        String val = username.getEditText().getText().toString();
        if(val.isEmpty()){
            username.setError("fields cannot be empty");
            return false;
        } else{
            username.setError(null);
            return true;
        }
    }

    private boolean validatePassword(){
        String val = password.getEditText().getText().toString();
        if(val.isEmpty()){
            password.setError("fields cannot be empty");
            return false;
        } else{
            password.setError(null);
            return true;
        }
    }

    private void checkUser(){

        final String userEnteredUsername = username.getEditText().getText().toString().trim();
        final String userEnteredPassword = password.getEditText().getText().toString().trim();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");

        Query checkUser = reference.orderByChild("username").equalTo(userEnteredUsername);

        checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if(dataSnapshot.exists()){

                    username.setError(null);
                    username.setErrorEnabled(false);

                    String passwordFromDB = dataSnapshot.child(userEnteredUsername).child("password").getValue(String.class);

                    if(passwordFromDB.equals(userEnteredPassword)){
                        Intent intent = new Intent( login.this, Dashboard.class);
                        startActivity(intent);
                    } else{
                        password.setError("wrong Password");
                        password.requestFocus();
                    }

                } else{
                    username.setError("no user found");
                    username.requestFocus();
                }

            }

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

            }
        });
    }

    public void loginUser(View View){
        if(!validateUsername() | !validatePassword()){
            return;
        }else {
            checkUser();
        }
    }

    public void regUser(View View){
        Intent intent = new Intent( login.this, Signup.class);
        startActivity(intent);
    }


}
  1. Error Log错误日志
2020-07-18 19:16:12.864 7246-7246/com.example.d1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.d1, PID: 7246
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.3.0:425)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.3.0:216)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.3.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.3.0:203)
        at com.example.d1.login$1.onDataChange(login.java:79)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.3.0:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0:55)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Here is the answer to your topic这是您的主题的答案

dataSnapshot.child(userEnteredUsername).child("password").getValue().toString()

instead of String.class而不是 String.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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