按标记设置和获取片段始终返回null

时间:2015-07-29 19:37:26

标签: java android android-fragments

所以我试图让2个片段通过父活动进行交流。 我实现了这样的接口和回调函数:

public class FormFragment extends Fragment {

    private EditText mStudentTextField;
    private EditText mStudentIndexField;
    private Button mSubmit;
    private String mStudentName;
    private int mStudentIndex;
    SendData sd;

    interface SendData {

        public void send(String msg);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.form_input, container, false);

        mStudentTextField = (EditText) view.findViewById(R.id.input_name);
        mStudentIndexField = (EditText) view.findViewById(R.id.input_index);
        mSubmit = (Button) view.findViewById(R.id.submit_button);

        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mStudentName = mStudentTextField.getText().toString();
                // mStudentIndex =
                // Integer.parseInt(mStudentIndexField.toString());

                sd.send(mStudentName);
                HomeFragment homeFragment = new HomeFragment();
                FragmentTransaction ft = getFragmentManager()
                        .beginTransaction();
                ft.replace(R.id.formFragment_Container, homeFragment,
                        "HOME_FRAGMENT").commit();
            }
        });

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            sd = (SendData) activity;
        } catch (ClassCastException c) {

            throw new ClassCastException(activity.toString()
                    + "Must implement send() method");
        }
    }
}

这就像一个中间立场。 FormFragments在MainActivity中获得第1个附加    并首先被调用。接下来,我从EditText中获取值,然后在按钮上单击我调用回调函数,该函数获得MainActivity的2个值(名称和索引)。因此,从FormFragment到Activity的数据工作。

这是我的HomeFragment应该从MainActivity获取数据并将其显示在正确的TextFields上。

HomeFragment controller:

public class HomeFragment extends Fragment {

    private TextView mName;
    private TextView mIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.home, container, false);

        mName = (TextView) view.findViewById(R.id.student_name);
        mIndex = (TextView) view.findViewById(R.id.student_index);

        return view;

    }

    public void getData(String msg) {

        mName.setText(msg);
    }
}

所以,你看。很简单。只需从args获取数据并使用它做一些事情。      现在,在MainActivity中,我遇到了一个我无法解决的问题,尽管在Android开发人员和github上浏览Stack Overflow和阅读文档。

看看我的MainActivity:

public class MainActivity extends Activity implements FormFragment.SendData {

    private String test;
    HomeFragment hf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        FormFragment formFragment = new FormFragment();

        ft.add(R.id.formFragment_Container, formFragment, "FORM_FRAGMENT")
                .addToBackStack(null).commit();

        hf = (HomeFragment) getFragmentManager().findFragmentByTag(
                "HOME_FRAGMENT"); // get a reference to a home fragment

    }

    @Override
    public void send(String msg) {

        hf.getData(msg);
    }
}

问题是hf是空引用,所以当我调用getData()时,它显然会崩溃。我相信我已经为两个片段正确设置了TAG名称,第一个片段在add()中,第二个片段在replace()中。这种行为背后的原因可能是什么。谢谢

1 个答案:

答案 0 :(得分:1)

ft.add(R.id.formFragment_Container,formFragment, "FORM_FRAGMENT").addToBackStack(null).commit();之后或甚至在调用fm.executePendingTransactions()之后,尚未添加片段,因为您只需在点击按钮后将其添加到片段管理器中mSubmit按钮OnClickListener

您可以在回调中包含主片段实例。或者您可以将片段添加到隐藏的片段管理器:ft.replace(R.id.formFragment_Container, homeFragment, "HOME_FRAGMENT").hide(homeFragment).commit();并仅在按下按钮时显示它。但我建议您考虑一下您的设计,可能有更好的选择。