在我现有的应用程序中,当创建ParentDialogFragment时,我使用getFragmentManager()进行活动,并在那里使用getChildFragmentManager创建嵌套的片段Dateselect_NestedDialogFragment。
应用程序正在运行,但是自Android 8.0-> 开始崩溃。我发现的最好的是"Why is Android O failing with “does not belong to this FragmentManager!”和"getParentFragment returning null",但是我仍然不知道如何在各处(以及哪一个)使用相同的fragmentmanager,因为我需要从嵌套的DialogFragment发送回数据到ParenDialogtFragment 。 对我的代码所做的更改应该在哪里进行?还是没有人知道指向Oreo-working sample-project的任何链接,在该链接中,数据从嵌套发送回父对话框片段?感谢您的任何建议=)
CODE snap:
---------------------------------------------------
In MainActivity I create 1st level DialogFragment:
FragmentManager fm = getFragmentManager();
ParentDialogFragment dialogFragment = ParentDialogFragment.newInstance(-1);
dialogFragment.show(fm, "NEW...");
-----------------------------------------------
In ParentDialogFragment I create 2nd level DialogFragment:
ft = getChildFragmentManager().beginTransaction();
//ft=getActivity().getFragmentManager().beginTransaction(); TRIED E.G. THIS TOO...
ft.addToBackStack(null);
newFragment = Dateselect_NestedDialogFragment.newInstance("klikattu_pvm");
newFragment.setTargetFragment(this, DIALOG_FRAGMENT_DATESELECT);
newFragment.show(ft, "dateselect_dialog");
...
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case DIALOG_FRAGMENT_DATESELECT:
if (resultCode == Activity.RESULT_OK)
{
if (data.getStringExtra("klikattu_pvm")!=null)
{
muokattavaHavainto.sHav_pvm = data.getStringExtra("klikattu_pvm");
blaablaa
}
}
break;
}
}
----------------------------------------
In Dateselect_NestedDialogFragment:
cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
{
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth)
{
if (cal.getDate() != date)
{
Intent intent = new Intent(getParentFragment().getActivity(), ParentDialogFragment.class);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
String sPvm = df.format(cal.getDate());
String sPrefname = getArguments().getString("dateprefname");
intent.putExtra(sPrefname, sPvm);
getParentFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
dismiss();
}
}
});
--------------------------------------------
尝试打开Dateselect_NestedDialogFragment->导致 java.lang.IllegalStateException:片段Dateselect_NestedDialogFragment {5d534a8#0 dateselect_dialog}声明目标片段ParentDialogFragment ...不属于此FragmentManager!
>更改为ft = getActivity()。getFragmentManager()。beginTransaction();在ParentDialogFragment->中导致'java.lang.NullPointerException:尝试在日期之后的Dateselect_NestedDialogFragment中的空对象引用上调用虚拟方法'android.app.Activity android.app.Fragment.getActivity()'在getParentFragment()。getActivity()处选择-p