Firebase无法将java.lang.String类型的值转换为long

时间:2017-11-01 17:21:22

标签: android firebase firebase-realtime-database

我试图在项目中创建一个登录。当我尝试登录时告诉我   com.google.firebase.database.DatabaseException:无法将java.lang.String类型的值转换为long。我不知道为什么我会嘲笑这些代码以及它对我来说似乎没什么好看

这是我的主要课程

package com.example.median1.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.app.ProgressDialog;
import android.widget.Toast;

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.ValueEventListener;

public class SignIn extends AppCompatActivity {
    EditText edtphone,edtPassword;
    Button btnSignIn;

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

        edtPassword=(EditText)findViewById(R.id.edtPassword);
        edtphone=(EditText)findViewById(R.id.edtPhone);
        btnSignIn=(Button)findViewById(R.id.btnSignIn);


        FirebaseDatabase database=FirebaseDatabase.getInstance();
        final DatabaseReference table_user=database.getReference("user");

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                final ProgressDialog mdialog=new ProgressDialog(SignIn.this);
                mdialog.setMessage("please wait....");
                mdialog.show();

                table_user.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                          //check if user dose not exist
                        if(dataSnapshot.child(edtphone.getText().toString()).exists()) {
                            //get user information

                            mdialog.dismiss();


                            user user1 = dataSnapshot.child(edtphone.getText().toString()).getValue(user.class);


                            if (user1.getPassword().equals(edtPassword.getText().toString())) {


                                Toast.makeText(SignIn.this, "sign in successfully", Toast.LENGTH_SHORT).show();
                            } else {

                                Toast.makeText(SignIn.this, "sign in is failed", Toast.LENGTH_SHORT).show();


                            }
                        }
                        else {
                            Toast.makeText(SignIn.this, "user dose not exist in Database", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });
    }

}

这是我从firebase获取字符串的类

package com.example.median1.demo;

/**
 * Created by median1 on 10/31/2017.
 */

public class user {
   private String Name;
   private String Password;

    public user() {
    }

    public user(String name, String password) {
        Name = name;
        Password = password;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

  

在firebase数据中,一个子节点具有字符串类型密码(" ala"),一个子节点具有Long(234)。   请制作字符串。