首页 > 解决方案 > 当我想使用之前添加的用户信息登录时,我的应用程序崩溃了。我用firebase数据库来写

问题描述

我正在使用 firebase 在 android studio 中编写登录页面。我可以添加一个用户并在我的 firebase 实时数据库中查看它。但是当我想登录时,它崩溃了,我真的不知道为什么。请帮忙。

这是我的加入活动。

public class JoinActivity extends AppCompatActivity {

    private EditText password1, phone1, name1;
    private Button CreateAccountbtn;

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

        CreateAccountbtn = (Button) findViewById(R.id.Register_btn);
        name1 = (EditText) findViewById(R.id.Name_btn);
        phone1 = (EditText) findViewById(R.id.edit_txt3);
        password1 = (EditText) findViewById(R.id.edit_txt4);


        CreateAccountbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreateAccount();
            }
        });
    }

    private void CreateAccount() {

        String name = name1.getText().toString();
        String password = password1.getText().toString();
        String phone = phone1.getText().toString();


        if(TextUtils.isEmpty(name)){

            Toast.makeText(getApplicationContext(), "please write your name", Toast.LENGTH_SHORT).show();

        }

        else if(TextUtils.isEmpty(password)){
            Toast.makeText(getApplicationContext(), "please write your password", Toast.LENGTH_SHORT).show();
        }

        else if(TextUtils.isEmpty(phone)){
            Toast.makeText(getApplicationContext(), "please enter your phone number", Toast.LENGTH_SHORT).show();
        }

        else{

            ValidatePhoneNumber(name, password, phone);


        }


    }

    private void ValidatePhoneNumber(final String name, final String password, final String phone) {

        final DatabaseReference rootRef;
        rootRef = FirebaseDatabase.getInstance().getReference();

        rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(!(dataSnapshot.child("Users").child(phone).exists())){

                    HashMap<String, Object> userdataMap = new HashMap<>();
                    userdataMap.put("phone", phone);
                    userdataMap.put("name", name);
                    userdataMap.put("password", password);

                    rootRef.child("Users").child(phone).updateChildren(userdataMap)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful()){
                                        Toast.makeText(getApplicationContext(), "your account has been created", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(JoinActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                    }
                                }
                            });

                }

                else{
                    Toast.makeText(getApplicationContext(), "this" + phone + "already exist", Toast.LENGTH_SHORT).show();

                }
            }

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

            }
        });
    }

}

这是我的登录活动。

public class LoginActivity extends AppCompatActivity {

    private EditText phone1, password1;
    private Button login_btn;
    private String parenDB = "Users";
    private ProgressDialog progressDialog;

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

        login_btn = (Button) findViewById(R.id.button3);
        phone1 = (EditText) findViewById(R.id.edit_txt1);
        password1 = (EditText) findViewById(R.id.edit_txt2);
        progressDialog = new ProgressDialog(this);

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

            }

        });


    }


    private void LoginUser() {


        String phone = phone1.getText().toString();
        String password = password1.getText().toString();


        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getApplicationContext(), "please write your password", Toast.LENGTH_SHORT).show();
        }

        else if (TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(), "please enter your phone number", Toast.LENGTH_SHORT).show();
        }

        else {

            progressDialog.setTitle("Logging in");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            AllowAccess(phone, password);

        }
    }



    private void AllowAccess(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(parenDB).child(phone).exists()){

                    Users userData = dataSnapshot.child(parenDB).getValue(Users.class);

                    //Users userData = dataSnapshot.getValue(Users.class);

                    if(userData.getPhone().equals(phone)){

                        if(userData.getPassword().equals(password)) {

                            Toast.makeText(LoginActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();

                            progressDialog.dismiss();

                            Intent intent2 = new Intent(LoginActivity.this, PassActivity.class);
                            startActivity(intent2);
                        }

                    }

                }

                else
                {
                    Toast.makeText(getApplicationContext(), "please create an account", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "process cancelled", Toast.LENGTH_SHORT).show();

            }
        });

    }

}

而且,这些是 gradle 依赖项:

implementation 'com.google.firebase:firebase-core:16.0.9'

implementation 'com.crashlytics.sdk.android:crashlytics:2.10.0'

implementation 'com.google.firebase:firebase-database:17.0.0'

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

标签: javaandroidfirebasefirebase-authentication

解决方案


我和你有同样的问题,但是在我将 firebase-database 版本从“17.0.0”降级到“16.0.4”之后,它对我来说很好。我不知道为什么会发生这种情况,所以我只是简单地将 firebase-database 版本更改为“implementation 'com.google.firebase:firebase-database:16.0.4'” 希望它有所帮助


推荐阅读