onResume() 时 DialogFragment 出现 NullPointerException

NullPointerException on DialogFragment at onResume()

我有一个 DialogFragment,它有这个特殊的 onResume 代码来处理后退按键。不幸的是,我的代码一直在生成 NullPointerException(我正在捕获它),但我不知道为什么会这样。每次打开 DialogFragment 时都会发生这种情况。感谢您对这个特殊案例的投入。

@Override
public void onResume() 
{
    super.onResume();

    try
    {
        getDialog().setOnKeyListener(new OnKeyListener()
        {
            @Override
            public boolean onKey(android.content.DialogInterface dialog, int keyCode,android.view.KeyEvent event) 
            {
                if ((keyCode == android.view.KeyEvent.KEYCODE_BACK)) 
                {
                    dismiss();
                    return true;
                }
                else return false; 
            }
        });
    }
    catch(NullPointerException e)
    {
        Log.w("NullPointerException", "HealthRecords.java : onResume()",e);
    }
}

我的LogCat错误:

01-21 15:47:20.266: W/NullPointerException(17010): HealthRecords.java : onResume()
01-21 15:47:20.266: W/NullPointerException(17010): java.lang.NullPointerException
01-21 15:47:20.266: W/NullPointerException(17010):  at digitalbeef.com.home.HealthRecords.onResume(HealthRecords.java:132)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.Fragment.performResume(Fragment.java:1743)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.BackStackRecord.run(BackStackRecord.java:684)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.FragmentManagerImpl.run(FragmentManager.java:443)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.os.Handler.handleCallback(Handler.java:733)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.os.Handler.dispatchMessage(Handler.java:95)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.os.Looper.loop(Looper.java:136)
01-21 15:47:20.266: W/NullPointerException(17010):  at android.app.ActivityThread.main(ActivityThread.java:5479)
01-21 15:47:20.266: W/NullPointerException(17010):  at java.lang.reflect.Method.invokeNative(Native Method)
01-21 15:47:20.266: W/NullPointerException(17010):  at java.lang.reflect.Method.invoke(Method.java:515)
01-21 15:47:20.266: W/NullPointerException(17010):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
01-21 15:47:20.266: W/NullPointerException(17010):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
01-21 15:47:20.266: W/NullPointerException(17010):  at dalvik.system.NativeStart.main(Native Method)

可能是因为在onCreateDialog之前调用了onResume。因此getDialogreturns无效。

您想要做的是在创建对话框后设置侦听器,甚至 在创建对话框时设置监听器:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(android.content.DialogInterface dialog, int keyCode,
                             android.view.KeyEvent event) {
            if ((keyCode == android.view.KeyEvent.KEYCODE_BACK)) {
                dismiss();
                return true;
            } else {
                return false;
            }
        }
    });

    return dialog;
}