从另一个 CustomDialogFragment 调用 CustomDialogFragment
Calling CustomDialogFragment from another CustomDialogFragment
你好吗?所以我在从另一个 cdf 调用我的自定义对话框片段 (cdf) 时遇到问题,当我从 FragmentActivity 调用 cdf 时它工作正常。
这是我用来从 FragmentActivity 调用 cdf 的方法。
private void openDatePicker(int idView) {
Bundle bundle = new Bundle();
bundle.putInt("VIEW", idView);
DialogFragment newFragment = new DatePicker();
newFragment.setArguments(bundle);
newFragment.show(getActivity().getFragmentManager(), "datePicker");
}
这是第一个cdf的onCreate方法
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_add, null);
ButterKnife.bind(this, view);
builder.setView(view);
当我从 cdf 调用它时,它给我一个错误
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我假设它与上下文有关,我的 cdf 不知道其他 cdf 的上下文,所以它不知道在哪里加载什么。那么我如何准确地从另一个 cdf 调用 cdf 呢?
将视图从一个片段传递到另一个片段不是正确的方法。当显示第二个片段时,第一个片段中的视图可能会被销毁,具体取决于您是否 add/replace 该片段。 Check the fragment lifecycle here in the android doc.
使用 setArguements
方法将值(而不是视图或视图 ID)作为 Bundle
传递给第二个片段。
你好吗?所以我在从另一个 cdf 调用我的自定义对话框片段 (cdf) 时遇到问题,当我从 FragmentActivity 调用 cdf 时它工作正常。 这是我用来从 FragmentActivity 调用 cdf 的方法。
private void openDatePicker(int idView) {
Bundle bundle = new Bundle();
bundle.putInt("VIEW", idView);
DialogFragment newFragment = new DatePicker();
newFragment.setArguments(bundle);
newFragment.show(getActivity().getFragmentManager(), "datePicker");
}
这是第一个cdf的onCreate方法
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_add, null);
ButterKnife.bind(this, view);
builder.setView(view);
当我从 cdf 调用它时,它给我一个错误
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我假设它与上下文有关,我的 cdf 不知道其他 cdf 的上下文,所以它不知道在哪里加载什么。那么我如何准确地从另一个 cdf 调用 cdf 呢?
将视图从一个片段传递到另一个片段不是正确的方法。当显示第二个片段时,第一个片段中的视图可能会被销毁,具体取决于您是否 add/replace 该片段。 Check the fragment lifecycle here in the android doc.
使用 setArguements
方法将值(而不是视图或视图 ID)作为 Bundle
传递给第二个片段。