我正在開發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);
}
可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix - 它) –
它不是我的情況下修復,因爲在android庫中發生異常而不是我的代碼 – mhmdawwad
你可以發佈一個片段,當你初始化你的'mDialog'變量嗎? – payloc