无法从片段创建FirebaseAuth

时间:2018-10-14 16:53:59

标签: android firebase firebase-authentication

我是android开发领域的新手。今天在这里,我想从片段中使用Firebase制作一个注册表单,这是我的代码

public class SignUpFragment extends Fragment {
private EditText username, mobile, email, password, repassword, address;
private Button btn_signUp;
private TextView toSignIn;
private FirebaseAuth mAuth;

private View view;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_sign_up, container, false);
    init();
    return view;
}

private void init() {

    email = (EditText) view.findViewById(R.id.upemail);
    password = (EditText) view.findViewById(R.id.uppassword);

    btn_signUp = (Button) view.findViewById(R.id.upsighnup);
    btn_signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // if (!repassword.getText().toString().equals(password.getText().toString())){
            //  Toast.makeText(getActivity(), "Password and confirm password not same. Please try again.", Toast.LENGTH_SHORT).show();
            //  return;

            String emailID = email.getText().toString();
            String paswd = password.getText().toString();
            if (emailID.isEmpty()) {
                email.setError("Provide your Email first!");
                email.requestFocus();
            } else if (paswd.isEmpty()) {
                password.setError("Set your password");
                password.requestFocus();
            }
            else if (emailID.isEmpty() && paswd.isEmpty()) {
                Toast.makeText(getActivity(), "Fields Empty!", Toast.LENGTH_SHORT).show();
            } else if (!(emailID.isEmpty() && paswd.isEmpty())) {

                mAuth.createUserWithEmailAndPassword(emailID, paswd).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Do your task in success
                        } else {
                            // Do your task in failure

                        }
                    }
                });
            }}});}
        }

这是在说Argument 'getActivity()' might be null 当我输入注册按钮应用程序崩溃时,我将提供日志记录

      10-14 22:11:54.153 14259-14259/com.example.guanzhuli.foody E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.guanzhuli.foody, PID: 14259
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(java.lang.String, java.lang.String)' on a null object reference
    at com.example.guanzhuli.foody.StartingPage.fragment.SignUpFragment$1.onClick(SignUpFragment.java:94)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
   10-14 22:11:54.154 14259-14259/com.example.guanzhuli.foody E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(java.lang.String, java.lang.String)' on a null object reference
    at com.example.guanzhuli.foody.StartingPage.fragment.SignUpFragment$1.onClick(SignUpFragment.java:94)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

请提供一些解决方案,我认为问题是从一个片段调用火力基地,有人请以正确的代码作为答案来帮助我...

1 个答案:

答案 0 :(得分:1)

Bro,您尚未初始化Auth。您应该将此行放在init()下:

mAuth = FirebaseAuth.getInstance();