获取null将String从片段传递给使用bundle的activiy

时间:2017-06-06 11:20:52

标签: android android-fragments bundle

这听起来很傻但是我使用bundle来存储我的Data,并试图将这个包从片段传递到托管活动,并使用toast测试数据,但是toast是空的!我无法找到发生这种情况的原因,因为我确保正确填写数据 片段中的代码是:

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        setUpViews();
        setRecyclerAdapter();
    }

    private void setUpViews() {
        fullNameEditText = (EditText) rootView.findViewById(R.id.fragment_parent_register_fullName);
        emailEditText = (EditText) rootView.findViewById(R.id.fragment_parent_register_email);
        passwordEditText = (EditText) rootView.findViewById(R.id.fragment_parent_register_password);
        fullName = fullNameEditText.getText().toString();
        email= emailEditText.getText().toString();
        password = passwordEditText.getText().toString();
        Bundle parentData = new Bundle();
        parentData.putString(KEY_PARENT_FULL_NAME,fullName);
        parentData.putString(KEY_PARENT_EMAIL,email);
        parentData.putString(KEY_PARENT_PASSWORD,password);

        Intent intent = getActivity().getIntent();
        intent.putExtras(parentData);
活动中的

是:

  registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DataHolder.getInstance().getAccountType() == 0) {
                    Intent intent = new Intent(RegisterActivity.this, TeacherLocationActivity.class);
                    startActivity(intent);
                } else {
                /*    Intent intent = new Intent(RegisterActivity.this, StudentLocationActivity.class);
                    startActivity(intent);*/
                    Bundle parentData = getIntent().getExtras();
                    if (parentData != null)
                    {
                        Toast.makeText(RegisterActivity.this, parentData.getString(KEY_PARENT_FULL_NAME)+parentData.getString(KEY_PARENT_EMAIL)+parentData.getString(KEY_PARENT_PASSWORD), Toast.LENGTH_SHORT).show();
                    }
                    else if (parentData == null)
                    {
                        Toast.makeText(RegisterActivity.this, "Bundle is empty", Toast.LENGTH_SHORT).show();                    }
                }
            }
        });

3 个答案:

答案 0 :(得分:0)

尝试从Fragment发送数据: -

Intent intent=new Intent(getContext(),YourActivityName.class);
            intent.putExtra(KEY_PARENT_FULL_NAME,fullName);
            intent.putExtra(KEY_PARENT_EMAIL,email);
            intent.putExtra(KEY_PARENT_PASSWORD,password);

尝试使用此方法获取活动中的数据: -

String data=this.getIntent().getStringextra(KEY_PARENT_FULL_NAME);

答案 1 :(得分:0)

要将数据从片段传递到活动,您应该使用OnFragmentInteractionListener。转到Android工作室,添加新片段以了解它是如何实现的。这很容易。

请在此处阅读:https://developer.android.com/training/basics/fragments/communicating.html

答案 2 :(得分:0)

SetupViews在创建视图后立即执行,这意味着它们最可能是空的。你需要首先让onClickListener在用户填写数据后设置数据。其他代码似乎是正确的