gpt4 book ai didi

java - android DialogFragment mDialog 为空导致异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:45 25 4
gpt4 key购买 nike

我正在开发安卓纸牌游戏,我正在使用 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 在该行为空mDialog.setContentView(view);

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);
}

最佳答案

通过执行以下操作不再显示此异常:

  1. 我将所有组件声明为字段成员而不是局部变量

    public class RentCardDialog extends DialogFragment {

    private static final String TAG = "RentCardDialog";

    private TextView title1;
    private TextView title2;
    private TextView title1TxtView;
    private TextView title2TxtView;
  2. 然后只需在 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);
  3. 最后,我添加了我想要的任何 fragment ,并按如下方式在 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 内部之前,所以问题可能是某些组件尚未真正加载,特别是 fragment ,因此它会触发该异常,

现在通过执行以下操作,它可以完美地工作。

关于java - android DialogFragment mDialog 为空导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44034789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com