Re: [问题] DialogFragment NullPointerException

楼主: darkk6 (Mr. Pan)   2018-08-30 20:09:32
※ 引述《asd860079 (U戏boy)》之铭言:
: 先上图和程式码讲一下我的程式在干嘛
: MainActivty:https://pastebin.com/513GqcXK
: DialogFram:https://pastebin.com/VnxCLPDy
: Dialog2:https://pastebin.com/G9S6J1Rn
拿到 NULL 很正常,你的逻辑是这样:
============================================
MainActivity 按下按钮的时候从 fragmentManager
找 Tag 为 "Dialog2" 的 fragment 并设为 DialogFragm 的 targetFragment
^^^^^^^^^^^^^^^^^^^ 问题在这里
在 DialogFragm 的 onAttach 时 getTargetFragment 并 assign 给 mOnInputListner
而按钮按下的时候执行
Dialog2 myDialog = new Dialog2();
mOnInputListner.sendInput(type);
myDialog.show(newFragment,"Dialog2");
============================================
问题是,你的 Dialog2 是在 DialogFragm 按下按钮才 show 的,
也就是那个时候才 attach 到 activity 上面
那么一开始在 MainActivity 从 fragmentManager 找 Tag 为 "Dialog2" 的时候
Dialog2 根本就没有被 attatch 在 activity 上,所以就不可能会有东西,
当下就会拿到 NULL , 也就是你 setTargetFragment 的时候那个 dialog2 = null
因此在 DialogFragm 的 onAttach 中 getTargetFragment 也是拿到 NULL

Links booklink

Contact Us: admin [ a t ] ucptt.com