DialogFragment.getDialog返回nullDialogFragment、getDialog、null

2023-09-12 07:48:26 作者:醉人与酒

我试图让该对话框我已经使用DialogFragment.getDialog(扩展DialogFragment创建),但它返回null。

I am trying to get the Dialog I have created with an extended DialogFragment using DialogFragment.getDialog() but it returns null.

基本上我想改变从创建并显示DialogFragment的FragmentActivity布局的文本。

Basically I want to alter the text in the layout from the FragmentActivity which creates and shows the DialogFragment.

推荐答案

您正在呼叫 getDialog()在DialogFragmen'ts生命周期为时尚早。

You're calling getDialog() too early in the DialogFragmen'ts life cycle.

getDialog()只需从DialogFragment返回私有变量 mDialog

getDialog() simply returns the private variable mDialog from the DialogFragment.

在DialogFragment被实例化 mDialog 为空,然后它被设置在 onCreateDialog 被激发内 getLayoutInflater(包savedInstanceState),所以你要叫 getDialog onCreateDialog

When a DialogFragment is instantiated mDialog is null, and then it gets set when onCreateDialog is fired inside getLayoutInflater(Bundle savedInstanceState), so you have to call getDialog after onCreateDialog.

例如,一些所谓的常用方法的顺序是的onCreate onCreateDialog onCreateView ONSTART 。所以,你可以叫 getDialog ,并将它返回的东西在 onCreateView ONSTART ,而不是在的onCreate onCreateDialog

For example, the order of some common methods called is onCreate, onCreateDialog, and onCreateView, onStart. So, you can call getDialog and have it return something in onCreateView or onStart, but not in onCreate or onCreateDialog.

Eventhough ONSTART 被称为called当片段是对用户可见 ,调整片段的布局在这一点上看起来不错....例如,使用设定的宽度和高度 getDialog ().getWindow()的setLayout(...,...); 不会使片段出现改变尺寸,但只是看上去有新设置的大小

Eventhough onStart is called called when the Fragment is visible to the user, adjusting the layout of the fragment at that point looks fine.... for example setting the width and height using getDialog().getWindow().setLayout(..., ...); doesn't make the fragment appear to change size, but just appears to have the newly set size.

 
精彩推荐