2017-05-17 127 views
3

我正在開發android紙牌遊戲,並且我使用DialogFragment來呈現一些圖像並告訴玩家對對手採取行動。android DialogFragment mDialog爲null導致異常

下面這段代碼被執行了很多次,沒有任何錯誤,但在讓我們說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(視圖)空;

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

我實例從這裏 對話框的錯誤發生在

布爾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); 
    } 
+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix - 它) –

+0

它不是我的情況下修復,因爲在android庫中發生異常而不是我的代碼 – mhmdawwad

+0

你可以發佈一個片段,當你初始化你的'mDialog'變量嗎? – payloc

回答

0

調試目的之前此例外不再通過執行以下操作來顯示:

  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. 最後我想補充我想要的任何片段和做所有對話框組件在BuildingActivity中創建如下

    @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裏面,所以這個問題可能是一些成分是不是真的加載尚未專門的片段,它觸發該異常,

現在

通過執行以下則可以正常使用。