Reputation: 133
I am a newbie in android development. here today i want to make a sign up form using firebase from a fragment here is my code
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
}
}
});
}}});}
}
It is saying something Argument 'getActivity()' might be null
when i enter the signup button app is crashing i will provide the logcat
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)
Please provide me some solution ,i think the problem is calling fire base from a fragment , someone please help me with the proper code as answer...
Upvotes: 1
Views: 1767
Reputation: 3527
Bro, you did not initialize the Auth. You should put this line under init()
:
mAuth = FirebaseAuth.getInstance();
Upvotes: 2