运维开发网

Java android DialogFragment mDialog是null导致异常

运维开发网 https://www.qedev.com 2020-03-10 13:14 出处:网络 作者:运维开发网整理
我正在开发 android卡片游戏,我正在使用DialogFragment,我在其中呈现一些图像并告诉玩家对对手采取行动. 下面的一段代码被执行多次,完全没有错误,但是让我们说5到10次之后发生以下异常. E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.arabdealgame.arabdealgame, PID: 8359
我正在开发 android卡片游戏,我正在使用DialogFragment,我在其中呈现一些图像并告诉玩家对对手采取行动.

下面的一段代码被执行多次,完全没有错误,但是让我们说5到10次之后发生以下异常.

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.arabdealgame.arabdealgame, PID: 8359
                                                                             java.lang.NullPointerException: Attempt to invoke Virtual method 'void android.app.Dialog.setOwnerActivity(android.app.Activity)' on a null object reference
                                                                                 at android.app.DialogFragment.onActivityCreated(DialogFragment.java:482)
                                                                                 at com.arabdealgame.activities.dialog.RentCardDialog.onActivityCreated(RentCardDialog.java:342)
                                                                                 at android.app.Fragment.performActivityCreated(Fragment.java:2362)
                                                                                 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1014)
                                                                                 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
                                                                                 at android.app.BackStackRecord.run(BackStackRecord.java:815)
                                                                                 at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
                                                                                 at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:563)
                                                                                 at com.arabdealgame.bo.Actions.RentActionCard$5.run(RentActionCard.java:315)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

上面的例外是告诉里面的

android.app.DialogFragment mDialog在该行为null

mDialog.setContentView(视图);

android.app.DialogFragment

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (!mShowsDialog) {
            return;
        }

        View view = getView();
        if (view != null) {
            if (view.getParent() != null) {
                throw new IllegalStateException("DialogFragment can not be attached to a container view");
            }
            mDialog.setContentView(view);
        }
        mDialog.setOwnerActivity(getActivity());

我从这里实例化对话框

错误发生在

boolean executePendingTransactions = fm.executePendingTransactions();

我在相关帖子中建议添加,以解决问题.

在添加此语句之前,错误发生时不会声明我的包下的任何类

RentActionCard.java

getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MyLog.i(TAG, "run: --------------------------1");
                FragmentManager fm = getActivity().getFragmentManager();
                MyLog.i(TAG, "run: --------------------------2");
                RentCardDialog rentCardDialog = new RentCardDialog();
                MyLog.i(TAG, "run: --------------------------3");
                if (!GameInfo.getCurrentPlayer().isUser()) {
                    MyLog.i(TAG, "run: --------------------------4");
                    rentCardDialog.setAgainstUser(true);
                }
                rentCardDialog.show(fm, "ccc");
                boolean executePendingTransactions = fm.executePendingTransactions();
                MyLog.d(TAG, "RentActionCard - executePendingTransactions : " + executePendingTransactions);
                MyLog.i(TAG, "run: --------------------------5");
            }
        });

在我的对话框中,为了调试目的,已重写此方法

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        MyLog.i(TAG, "onActivityCreated: ----------------------------------");
        super.onActivityCreated(savedInstanceState);
    }
执行以下操作不再显示此异常:

>我将所有组件声明为字段成员而非局部变量

public class RentCardDialog extends DialogFragment {

private static final String TAG = "RentCardDialog";

private TextView title1;
private TextView title2;
private TextView title1TxtView;
private TextView title2TxtView;

>然后只需在onCreateView中初始化它们,如下所示

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.dialog_rent_card, container, false);

title1 = (TextView) rootView.findViewById(R.id.rent_card_dialog_title1);
title2 = (TextView) rootView.findViewById(R.id.rent_card_dialog_title2);
title1TxtView = (TextView) rootView.findViewById(R.id.rent_card_dialog_title1);
title2TxtView = (TextView) rootView.findViewById(R.id.rent_card_dialog_title2);

>最后,我添加了我想要的任何片段,并在onActivityCreated中进行所有对话框组件构建,如下所示

@Override  
 public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);  // Will now complete and not crash

try {
    buildUserOptions();
    MyLog.i(TAG, "onCreateView: ---------------------------3");

    player2Fragment.setExecuteCallback(new CallbackInterface() {
        @Override
        public void execute() {
            player2Fragment.refreshPlayerData(player2);
        }
    });

在对话框的业务工作进入onCreateView之前所以问题可能是某些组件没有真正加载但特别是片段因此它触发了该异常,

现在通过以下方式完成工作.

0

精彩评论

暂无评论...
验证码 换一张
取 消