如何让firebase中的updateprofile工作?

时间:2017-02-25 12:40:23

标签: android firebase firebase-authentication

我试图让用户在创建ID时添加displayname。

我设置了让用户在第一个屏幕上显示他们的ID,然后当你点击下一个它创建ID时,它会进入更新配置文件页面以更新显示名称。

我已经完成了所有工作但是当我介绍第二个更新页面时,当我被定向到该页面时它会崩溃。

注册页面

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
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.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;

public class signup extends AppCompatActivity implements View.OnClickListener {

    //defining view objects
    private EditText editTextEmail;
    private EditText editTextPassword;
 //   private EditText editTextName;
    private Button buttonSignup;

    private TextView textViewSignin;

    private ProgressDialog progressDialog;


    //defining firebaseauth object
    private FirebaseAuth firebaseAuth;

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

        //initializing firebase auth object
        firebaseAuth = FirebaseAuth.getInstance();

        //if getCurrentUser does not returns null
        if(firebaseAuth.getCurrentUser() != null){
            //that means user is already logged in
            //so close this activity
            finish();

            //and open profile activity
            startActivity(new Intent(getApplicationContext(), profile.class));
        }

        //initializing views
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        textViewSignin = (TextView) findViewById(R.id.textViewSignin);
      //  editTextName = (EditText) findViewById(R.id.editTextName);

        buttonSignup = (Button) findViewById(R.id.buttonSignup);

        progressDialog = new ProgressDialog(this);

        //attaching listener to button
        buttonSignup.setOnClickListener(this);
        textViewSignin.setOnClickListener(this);
    }

    private void registerUser(){

        //getting email and password from edit texts
        String email = editTextEmail.getText().toString().trim();
        String password  = editTextPassword.getText().toString().trim();

        //checking if email and passwords are empty
        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;
        }

        //if the email and password are not empty
        //displaying a progress dialog

        progressDialog.setMessage("Working Please Wait...");
        progressDialog.show();

        //creating a new user
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        //checking if success
                        if(task.isSuccessful()){

                            finish();

                            startActivity(new Intent(getApplicationContext(), secondsignup.class));
        //                    Toast.makeText(signup.this,"Registration Successful",Toast.LENGTH_SHORT).show();

                        }else{
                            //display some message here
                            Toast.makeText(signup.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();
                    }
                });

    }

    @Override
    public void onClick(View view) {

        if(view == buttonSignup){
            registerUser();
        }

        if(view == textViewSignin){
            //open login activity when user taps on the already registered textview
            startActivity(new Intent(this, mainlogin.class));
        }

    }
}

secondsignup page

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.Toast;
import android.view.View.OnClickListener;
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.auth.UserProfileChangeRequest;



public class secondsignup extends AppCompatActivity implements OnClickListener {

    private EditText mUsername;
    private Button mUpdateProfileBtn;
    FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();


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

        mUsername = (EditText) findViewById(R.id.editTextName);
        mUpdateProfileBtn = (Button) findViewById(R.id.buttonSignin);
        String username = user.getDisplayName();
        mUsername.setText(username);

        mUpdateProfileBtn.setOnClickListener(this);
    }

    //       @Override
    //       public void onClick(View v) {
    private void updatename() {
        //updating user's profile data
        String nameUser = mUsername.getText().toString();
        UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                .setDisplayName(nameUser)
                .build();

        if (TextUtils.isEmpty(nameUser)) {
            mUsername.setError("Enter your Full Name");
        }

        //if the field is not null, process continue to update profile
        else {
            mUser.updateProfile(profileUpdate)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(Task<Void> task) {
                            if (task.isSuccessful()) { //success on updating user profile
                                Toast.makeText(secondsignup.this, "Registration Successful",
                                        Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(secondsignup.this, MainActivity.class));
                            } else { //failed on updating user profile
                                Toast.makeText(secondsignup.this, "Registration Failed",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }

    }

    @Override
    public void onClick(View view) {

        if (view == mUpdateProfileBtn) {
            updatename();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

只需像这样更改onClick()方法

@Override
public void onClick(View view) {

    if (view.getId == R.id.buttonSignin) {
        updatename();
    }
}