如何在父片段而不是父活动中正确获取日期选取器值



我有复杂的应用程序,有大量的片段和子片段,我需要听众在片段中收听,而不是在活动中收听。这通常有效,但不适用于日期或时间选择器。

下面是一个活动膨胀的示例应用程序 - TestFragmentMain。膨胀的片段还有两个片段 - 一个具有单个EditText(TestFragment_InputBox(,另一个具有单个TextView(TestFragment_DateBox(,它用于侦听事件(TextChangeListener和DateChangeListener(。

在第一个片段中的文本更改时,所有工作都像魅力一样,主片段正在接收结果。

但是,在日期更改时,我收到并错误:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.gmail.xxx.xxx.test.DateChangeListener.dateChanged(int, int, int)' on a null object reference
at com.gmail.xxx.xxx.test.TestFragment_DatePicker.onDateSet(TestFragment_DatePicker.java:32)

我真的不明白为什么。任何帮助,不胜感激。

主要活动:

public class TestClass extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
TestFragmentMain testFragmentMain = new TestFragmentMain();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.test_fragment_container, testFragmentMain);
ft.commitAllowingStateLoss();
}
}

主片段,带侦听器

public class TestFragmentMain extends Fragment implements TextChangeListener, DateChangeListener  {
@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
TestFragment_InputBox  testFragmentInputBox = new TestFragment_InputBox();
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.test_fragment_inputbox_container, testFragmentInputBox);
ft.commitAllowingStateLoss();
TestFragment_DateBox  testFragmentDateBox = new TestFragment_DateBox();
ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.test_fragment_date_container, testFragmentDateBox);
ft.commitAllowingStateLoss();
return view;
}
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
}
@Override
public void textChanged() {
Log.d("LISTEN","Text has been changed...");
}
@Override
public void dateChanged(int year, int month, int day) {
Log.d("LISTEN","Date has been changed to ...");
}
}

带输入框的片段:

public class TestFragment_InputBox extends Fragment {
private TextChangeListener textChangeListener;
@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_input_box, container, false);
EditText editText = view.findViewById(R.id.input_view);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
textChangeListener.textChanged();
}
});
return view;
}
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
try {
textChangeListener = (TextChangeListener) getParentFragment();
} catch (ClassCastException e) {
e.printStackTrace();
}
}
}

带有日期框的片段:

public class TestFragment_DateBox extends Fragment implements DateChangeListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_date_box, container, false);
TextView textView = view.findViewById(R.id.date_view);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new TestFragment_DatePicker();
datePicker.show(getActivity().getSupportFragmentManager(), "datePicker");
}
});
return view;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void dateChanged(int year, int month, int day) {
Log.d("LISTEN","Date has been changed in box fragment with box ...");
}
}

日期选取器片段

public class TestFragment_DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public DateChangeListener dateChangeListener;
@NotNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(Objects.requireNonNull(getContext()), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
dateChangeListener.dateChanged(year,month,day);
}
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
try {
dateChangeListener = (DateChangeListener) getParentFragment();
} catch (ClassCastException e) {
e.printStackTrace();
}
}
}

和听众:

public interface TextChangeListener {
void textChanged();
}
public interface DateChangeListener {
void dateChanged(int year,int month,int day);
}

您的片段TestFragment_DateBox调用日期选取器并继承DatePickerDialog.OnDateSetListener

public class TestFragment_DateBox extends Fragment implements DatePickerDialog.OnDateSetListener

并像这样显示日期选择器片段

TestFragment_DatePicker().show(this.childFragmentManager, "datepicker")

并覆盖日期集

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// As you are in the calling fragment you can use the values as you see fit
TextView textView = view.findViewById(R.id.date_view);
textView.text = year.toString()
}

TestFragment_DatePicker有一个听众

private lateinit var listener: DatePickerDialog.OnDateSetListener

然后覆盖附加

override fun onAttach(context: Context) {
super.onAttach(context)
// Verify that the dialog parent implements the callback interface
try {
// Instantiate the OnDateSetListener so we can send events to the host
listener = parentFragment as DatePickerDialog.OnDateSetListener
} catch (e: ClassCastException) {
// The parent doesn't implement the interface, throw exception
throw ClassCastException(("$context must implement OnDateSetListener"))
}
}

很抱歉 java 和 kotlin 的混合,但你明白了!

问题是你没有给TestFragment_DatePicker膨胀,这是一个DialogFragment,这是一个Fragment,而是返回一个DatePickerDialog,这是一个AlertDialog,这是一个没有mParentFragmentDialog。这就是getParentFragment()返回 null 的原因。

这看起来像是我们从旧版本的 android 继承而来的糟糕架构设计。

我找到了解决方案。

在TestFragmentMain中,TestFragment_DateBox需要使用与DatePicker相同的FragmentManager进行交易。这是TestFragmentMain中的代码:

TestFragment_DateBox testFragmentDateBox = new TestFragment_DateBox();
FragmentTransaction  ft2 = this.getChildFragmentManager().beginTransaction();
ft2.replace(R.id.test_fragment_date_container, testFragmentDateBox);
ft2.commitAllowingStateLoss();

打开日期选取器时,更改也在TestFragment_DateBox:

DialogFragment datePicker = new TestFragment_DatePicker();
datePicker.setTargetFragment(this, 0);
datePicker.show(this.getFragmentManager(), "datePicker");

我们需要运行 setTargetFragment 才能工作。

现在这个测试有效。

相关内容

  • 没有找到相关文章

最新更新