在DialogFragment' onResume中调用getDialog.getWindow时的NPE

时间:2016-04-18 03:13:50

标签: android dialogfragment

在我的DialogFragment中,我只是这样做:

    override fun onResume() {
        super.onResume()
        dismissDialog()
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dpToPx(220f, resources));
        getDialog().getWindow().setGravity(Gravity.BOTTOM)
        getDialog().setCanceledOnTouchOutside(false)
    }

但我得到了NPE:

Unable to resume activity {PublishActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Dialog.getWindow()' on a null object reference
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3308)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3344)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5773)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:799)

在我的活动中,我启动了一个DialogFragment并遇到了这个错误。它发生了好几次。

1 个答案:

答案 0 :(得分:1)

如果getDialog()不为空,则应检查onViewCreated/onCreate是否应将LayoutParameters复制到Resume,并将代码保存在NPE中当您检查 if(getDialog() !=null){ dismissDialog() getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dpToPx(220f, resources)); getDialog().getWindow().setGravity(Gravity.BOTTOM) getDialog().setCanceledOnTouchOutside(false) }

import socket

host = '127.0.0.1'
port = 8000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)

conn, addr = s.accept()

while True:
    data = conn.recv(1024)
    print 'Received:', data
    if not data:
        break
    conn.sendall(data)
    print 'Sent:', data

conn.close()