Java – Android dialogfragment mdialog is null, causing an exception

I'm developing an Android card game. I'm using dialogfragment. I present some images and tell players to take action against their opponents

The following code is executed many times without error, but let's say that the following exception occurs after 5 to 10 times

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)

The above exception is to tell Android app. Dialogfragment mdialog is null mdialog in this behavior 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());

I instantiate the dialog box from here. The error occurred in

boolean executePendingTransactions = fm. executePendingTransactions(); I suggest adding in related posts to solve the problem Before adding this statement, no classes under my package will be declared when an error occurs

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

In my dialog box, this method has been overridden for debugging purposes

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        MyLog.i(TAG,"onActivityCreated: ----------------------------------");
        super.onActivityCreated(savedInstanceState);
    }

Solution

Do the following to not show this exception again:

>I declare all components as field members rather than local variables

public class RentCardDialog extends DialogFragment {

private static final String TAG = "RentCardDialog";

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

>Then simply initialize them in oncreateview, as shown below

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

>Finally, I added any fragment I wanted and built all the dialog components in onactivitycreated, as shown below

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

Before the business work of the dialog box enters oncreateview, the problem may be that some components are not really loaded, especially fragments, so it triggers the exception,

Now complete the work in the following ways

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>