1

I am very new to Android and a high school student. So I think it'd be nice if you could give me the code, too. I implemented login using firebase and LoginActivity.FragmentIndivative in java.I want to send my name, e-mail address, and phone number to java. So I used a Bundle object, and NullpointerException occurred. Please help me. I don't have time. I'll wait for you guys. I'll attach the code below. Please take good care of me.

private void signUp(){
    final String email = ((EditText)findViewById(R.id.idText)).getText().toString();
    final String password = ((EditText)findViewById(R.id.pwText)).getText().toString();

    if(email.length() > 0 && password.length() > 0){
        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Intent intent1;
                        if (task.isSuccessful()) {
                            FirebaseUser user = mAuth.getCurrentUser();
                            startToast("로그인에 성공하였습니다");

                            fragmentIndividual = new FragmentIndividual();
                            FragmentManager fm = getSupportFragmentManager();
                            FragmentTransaction fmt = fm.beginTransaction();

                            intent1 = new Intent(getApplicationContext(), MainActivity.class);
                            intent1.putExtra("name", "nnnaa");
                            Bundle bundle = new Bundle();
                            bundle.putString("name", mAuth.getCurrentUser().getDisplayName());
                            bundle.putString("email", mAuth.getCurrentUser().getEmail());
                            bundle.putString("phone", mAuth.getCurrentUser().getPhoneNumber());

                            System.out.println(email);
                            System.out.println(mAuth.getCurrentUser().getEmail());

                            startActivity(intent1);
                        } else {
                            if(task.getException() != null){
                                startToast(task.getException().toString());
                            }
                        }
                    }
                });
    }else{
        startToast("빈 칸을 채워주세요");
    }
}

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_individual, container, false);

    Bundle bundle = this.getArguments();
    if(bundle != null){
        bundle = getArguments();
        String name = bundle.getString("name");
        String email = bundle.getString("email");
        String phone = bundle.getString("phone");

        View v = inflater.inflate(R.layout.fragment_individual, container, false);
        nameTv = (TextView)v.findViewById(R.id.nameTv);
        emailTv = (TextView)v.findViewById(R.id.emailTv);
        phoneTv = (TextView)v.findViewById(R.id.phoneTv);

        nameTv.setText(name);
        emailTv.setText(email);
        phoneTv.setText(phone);
    }

    return view;
}
zyoon
  • 37
  • 2

1 Answers1

1

Inside your onComplete method, Try to replace inside your if statement with this code.

if (task.isSuccessful()) {
   FirebaseUser user = mAuth.getCurrentUser();
   startToast("로그인에 성공하였습니다");
   Bundle bundle = new Bundle();
   bundle.putString("name", mAuth.getCurrentUser().getDisplayName());
   bundle.putString("email", mAuth.getCurrentUser().getEmail());
   bundle.putString("phone", mAuth.getCurrentUser().getPhoneNumber());

  System.out.println(email);
  System.out.println(mAuth.getCurrentUser().getEmail());
      
  Fragment fragment = IndividualFragment.newInstance(bundle);

  FragmentManager manager = getSupportFragmentManager();
  FragmentTransaction transaction = manager.beginTransaction();

  transaction.add(R.id.frame_layout, fragment, "fragment").commit();
} else {
...

where R.id.your_activity_layout is your Activity layout or you can either add a FrameLayout inside your activity layout .xml.

And in your IndividualFragment

public class IndividualFragment extends Fragment {

  public IndividualFragment() {

  }

  private TextView nameTv;
  private TextView emailTv;
  private TextView phoneTv;

  public static IndividualFragment newInstance(Bundle bundle) {
    IndividualFragment fragment = new IndividualFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_individual, container, false);

    Bundle bundle = getArguments();

    if(bundle != null){
      bundle = getArguments();
      String name = bundle.getString("name");
      String email = bundle.getString("email");
      String phone = bundle.getString("phone");

      nameTv = (TextView)view.findViewById(R.id.nameTv);
      emailTv = (TextView)view.findViewById(R.id.emailTv);
      phoneTv = (TextView)view.findViewById(R.id.phoneTv);

      nameTv.setText(name);
      emailTv.setText(email);
      phoneTv.setText(phone);
    }

    return view;
  }
}

You can also check this link. https://stackoverflow.com/a/36100397/11445765

  • Thank you for your answer. But it doesn't work even though I did the way you told me. I'll attach the code again, so can you tell me how? Please. I don't have time. – zyoon Oct 24 '20 at 11:33
  • Bundle is still null – zyoon Oct 24 '20 at 11:34
  • Thank you for your answer. But it doesn't work even though I did the way you told me. I'll attach the code again, so can you tell me how? Please. We don't have time. Bundle is still null – zyoon Oct 24 '20 at 11:51
  • You're trying to put those firebase data to a bundle and starting a new activity, And this gives you a NullPointerException 'cause you're trying to get the data thru a fragment. – forcestrong123 Oct 24 '20 at 12:15
  • I bet you're trying to retrieve the data from Activity to Fragment? Am I right? – forcestrong123 Oct 24 '20 at 12:19
  • Yes, but I don't know why it doesn't work. And I'm Korean. Please understand even if I'm not good at English. – zyoon Oct 24 '20 at 12:36