如何使用android studio同时在firebase中验证和保存数据

时间:2017-06-30 18:45:49

标签: firebase

我已经写了一个代码并且收到了错误 06-30 23:55:00.702 2499-2499 / com.example.parthtiwari.trace E / AndroidRuntime:FATAL EXCEPTION:main                                                                              处理:com.example.parthtiwari.trace,PID:2499                                                                              java.lang.NullPointerException:尝试在null上调用虚方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'

我的代码是

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.AuthResult;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity implements 

View.OnClickListener {
    private FirebaseAuth fb;
    private DatabaseReference databaseReference;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private EditText editTextName, mobile_number,vehicle_number,editTextAddress;
    private Button buttonSignup;
    private TextView textViewSignin;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fb = FirebaseAuth.getInstance();

        if(fb.getCurrentUser() != null){
            finish();
            startActivity(new Intent(getApplicationContext(),welcome.class));
        }

        databaseReference = FirebaseDatabase.getInstance().getReference();
        editTextAddress = (EditText) findViewById(R.id.Address);
        editTextName = (EditText) findViewById(R.id.name);
        mobile_number = (EditText) findViewById(R.id.mobile);
        vehicle_number = (EditText) findViewById(R.id.vehicle);
        editTextEmail = (EditText) findViewById(R.id.Email);
        editTextPassword = (EditText) findViewById(R.id.Password);
        buttonSignup = (Button) findViewById(R.id.Signup);
        textViewSignin = (TextView) findViewById(R.id.Signin);
        progressDialog = new ProgressDialog(this);
        FirebaseUser user = fb.getCurrentUser();
        buttonSignup.setOnClickListener(this);
        textViewSignin.setOnClickListener(this);
    }

    private void registerUser() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "Please enter email", 
Toast.LENGTH_LONG).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please enter password", 
Toast.LENGTH_LONG).show();
            return;
        }
        progressDialog.setMessage("Registering Please Wait...");
        progressDialog.show();
        fb.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Successfully registered", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();

                    }
                });
        saveUserInformation();
    }

    private void saveUserInformation() {
        String name = editTextName.getText().toString().trim();
        String mob = mobile_number.getText().toString().trim();
        String vehicle = vehicle_number.getText().toString().trim();
        String add = editTextAddress.getText().toString().trim();
        //creating a userinformation object
        userinfo userInformation = new userinfo(name, mob, vehicle, add);
        //getting the current logged in user
        FirebaseUser user = fb.getCurrentUser();
        databaseReference.child(user.getUid()).setValue(userInformation);
        //displaying a success toast
        Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View view) {
        if(view == buttonSignup){
            registerUser();

        }
        if(view == textViewSignin){
            startActivity(new Intent(this, login.class));

        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将saveUserInformation()移动到onCompletionListener中。

看起来在注册完成之前调用了saveUserInformation()。

 fb.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
         saveUserInformation(authResult);
   Toast.makeText(MainActivity.this, "Successfully 
  registered",Toast.LENGTH_LONG).show();
  progressDialog.dismiss(); }).addOnFailureListener(new OnFailureListener() { 
 @Override
  public void onFailure(@NonNull Exception e) {
 Toast.makeText(MainActivity.this, "Registration Error", 
 Toast.LENGTH_LONG).show();
  progressDialog.dismiss();
            });

并更改

saveUserInformation(AuthResult authResult)

authResult.getUid();