4

I am developing android card game, and i am using DialogFragment where I render some images and tell the player to take action against opponent.

the following piece of code gets executed many times with no errors at all but after lets say 5 to 10 times the following exception occurred.

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 telling that inside android.app.DialogFragment mDialog is null at the line 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());

I am instantiating the dialog from here the error occur at

boolean executePendingTransactions = fm.executePendingTransactions(); which I added as suggested in a related post in order to fix the problem. before adding this statement the error occur without stating any class under my package

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

this method has been overridden for debugging purposes inside my dialog

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

1 Answers1

0

This exception is no longer showed up by doing the following :

  1. I declared all the componenet as field member rather 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;
    
  2. then just initialize them inside onCreateView as follows

    @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. Finally I add whatever fragment I want and do all the dialog component building inside onActivityCreated as follows

     @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 dialog's business work was inside the onCreateView so the problem probably was some component wasn't really loaded yet specially the fragments so it fires that exception,

now by doing the following it is working perfectly.

mhmdawwad
  • 41
  • 4