安卓:如何将数据从活动发送到片段(错误:bundle为空)



我是安卓系统的新手,还是一名高中生。所以我觉得如果你也能给我代码就好了。我使用firebase和LoginActivity实现了登录。java中的FragmentIndivative。我想把我的名字、电子邮件地址和电话号码发送到java。所以我使用了Bundle对象,出现了NullpointerException。请帮帮我,我没有时间。我等你们。我将在下面附上代码。请好好照顾我。

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;
}

在onComplete方法内部,尝试用以下代码替换if语句内部。

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 {
...

其中R.id.your_activity_layout是您的活动布局,或者您可以在活动布局.xml. 中添加FrameLayout

并且在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;
  }
}

您也可以查看此链接。https://stackoverflow.com/a/36100397/11445765