dialog.getWindow()返回空指针异常

时间:2018-01-31 02:48:42

标签: android

这是我的代码:

private void handleShow() {
    awakenIfNecessary();
    mDialog = createDialog();
    prepareDialog();

    // If we only have 1 item and it's a simple press action, just do this action.
    if (mAdapter.getCount() == 1
            && mAdapter.getItem(0) instanceof SinglePressAction
            && !(mAdapter.getItem(0) instanceof LongPressAction)) {
        ((SinglePressAction) mAdapter.getItem(0)).onPress();
    } else {
        if (mDialog != null) {
            WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
            attrs.setTitle("LegacyGlobalActions");
            mDialog.getWindow().setAttributes(attrs);
            mDialog.show();
            mDialog.getWindow().getDecorView().setSystemUiVisibility(
                    View.STATUS_BAR_DISABLE_EXPAND);
        }
    }
}
来自AOSP的LegacyGlobalActions.java,我做了一个非空判断,但是当运行猴子测试时,发生了崩溃:

  

01-29 23:55:04.201 3378 3574 E AndroidRuntime:***系统进程中的致命异常:android.ui   01-29 23:55:04.201 3378 3574 E AndroidRuntime:java.lang.NullPointerException:尝试调用虚拟方法' android.view.Window com.android.server.policy.LegacyGlobalActions $ GlobalActionsDialog.getWindow()&# 39;在null对象引用上   01-29 23:55:04.201 3378 3574 E AndroidRuntime:at com.android.server.policy.LegacyGlobalActions.handleShow(LegacyGlobalActions.java:210)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:at com.android.server.policy.LegacyGlobalActions.showDialog(LegacyGlobalActions.java:178)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:at com.android.server.policy.GlobalActions $ 1.run(GlobalActions.java:108)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:790)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:99)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:在android.os.Looper.loop(Looper.java:164)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:在android.os.HandlerThread.run(HandlerThread.java:65)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:at com.android.server.ServiceThread.run(ServiceThread.java:46)   01-29 23:55:04.201 3378 3574 E AndroidRuntime:at com.android.server.UiThread.run(UiThread.java:42)   5B5C88 01-29 23:55:05.734 20508 20508 E AndroidRuntime:致命异常:主要

如果有人能告诉我原因,我将非常感激!

1 个答案:

答案 0 :(得分:0)

你试试

private void handleShow() {
    awakenIfNecessary();
    mDialog = createDialog();
    prepareDialog();

    // If we only have 1 item and it's a simple press action, just do this action.
    if (mAdapter.getCount() == 1
            && mAdapter.getItem(0) instanceof SinglePressAction
            && !(mAdapter.getItem(0) instanceof LongPressAction)) {
        ((SinglePressAction) mAdapter.getItem(0)).onPress();
    } else {
        if (mDialog != null) {   
            mDialog.show();
WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
            attrs.setTitle("LegacyGlobalActions");
            mDialog.getWindow().setAttributes(attrs);
            mDialog.getWindow().getDecorView().setSystemUiVisibility(
                    View.STATUS_BAR_DISABLE_EXPAND);
        }
    }
}