数据已更新到Firebase中,但应用在Android中崩溃

时间:2019-05-04 14:26:50

标签: java android firebase

当我单击按钮尝试在Firebase数据库中更新用户时,我的应用程序崩溃,但结果进入了数据库。

我的更新功能:

private void updateUser() {
        final String firstname = et_firstname.getText().toString().trim();
        final String lastname = et_lastname.getText().toString().trim();
        final String number = et_number.getText().toString().trim();

        if (firstname.isEmpty()) {
            et_firstname.setError(getText(R.string.text_error_enter_firstname));
            et_firstname.requestFocus();

            return;
        }
        if (lastname.isEmpty()) {
            et_lastname.setError(getText(R.string.text_error_enter_lastname));
            et_lastname.requestFocus();

            return;
        }
        if (number.isEmpty()) {
            et_number.setError(getText(R.string.text_error_number));
            et_number.requestFocus();

            return;
        }

        User user = new User(firstname, lastname, firebaseUser.getEmail(), number);

        databaseReference
                .child(firebaseUser.getUid())
                .setValue(user).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Toast.makeText(container, getText(R.string.text_success_updated_profile), Toast.LENGTH_LONG).show();
            }
        });

    }

我在OnCreate方法中声明了databaseReference:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        container = getActivity();
        databaseReference = FirebaseDatabase.getInstance().getReference("Users");
    }

firebaseUser也声明:

firebaseUser = mAuth.getCurrentUser();

我做了EditText填充功能来获取用户详细信息:

private void fillEditText() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.child(firebaseUser.getUid()).getValue(User.class);

                et_firstname.setText(user.getFirstname());
                et_lastname.setText(user.getLastname());
                et_number.setText(user.getContactno());
            }

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

            }
        });
    }

我的用户类别:

public class User {
    private String firstname, lastname, email, contactno;

    public User() {
    }

    public User(String firstname, String lastname, String email, String contactno) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.contactno = contactno;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContactno() {
        return contactno;
    }

    public void setContactno(String contactno) {
        this.contactno = contactno;
    }
}

如果我在EditText中进行了更改,然后单击“保存”按钮-如果未进行任何更改,我的应用程序将崩溃-会出现Toast消息。

0 个答案:

没有答案