java.lang.NullPointerException:for getDialog()。getWindow()

时间:2017-07-27 07:25:36

标签: android android-fragments android-dialogfragment dialogfragment

我收到此错误消息

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Dialog.getWindow()' on a null object reference

我在课堂上尝试这个

public class ABCD_Fragment extends DialogFragment {

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        super.onViewCreated(view, savedInstanceState);
    }

}

请帮帮我。我不明白为什么getDialog()为空。

1 个答案:

答案 0 :(得分:0)

如果您从片段中调用它,请使用此选项:

FragmentManager fragmentManager = getFragmentManager();
                 ABCD_Fragment mDialogwindow = new ABCD_Fragment();
                 mDialogwindow.show(fragmentManager, "DIALOG WINDOW");

如果您从活动中调用它:

FragmentManager fragmentManager = getSupportFragmentManager();
          ABCD_Fragment mDialogwindow = new ABCD_Fragment(); 
mDialogwindow.show(fragmentManager, "DIALOG WINDOW");

然后在你的ABCD_FRAGMENT中添加:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        super.onViewCreated(view, savedInstanceState);