即使在删除了错误的代码行之后,错误仍然存​​在

时间:2016-08-23 23:19:09

标签: android

我正在开发一款能够保持纸牌游戏得分的应用程序,直到现在它才能正常运行。我有几个地方可以检查以确保输入的数字不会导致任何错误,也就是说,它们不会违反游戏规则。如果遇到此类输入,应用程序会打开AlertDialog告诉用户该问题。但是,我曾尝试致电getDialog().setCanceledOnTouchOutside(false),因为我使用NullPointerException创建了AlertDialog,因此AlertDialog.Builder投了AlertDialog。我删除了该声明,但每次尝试运行该应用并故意弹出NullPointerException时,AlertDialog消息都会显示,应用程序崩溃。

以下是创建@Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(getString(R.string.title_error)) .setMessage(errorText) .setCancelable(false); switch (errorType) { case 1: builder.setPositiveButton(getString(R.string.text_OK), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { PlayerNumErrorDialogListener activity = (PlayerNumErrorDialogListener) getActivity(); activity.onFinishPlayerNumError(); dialog.dismiss(); } }); break; case 2: builder.setPositiveButton(getString(R.string.text_OK), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { CurrentTotalScoreErrorDialogListener activity = (CurrentTotalScoreErrorDialogListener) getActivity(); activity.onFinishCurrentTotalScoreError(); } }); break; case 3: builder.setPositiveButton(getString(R.string.text_OK), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { PlayerScoreErrorDialogListener activity = (PlayerScoreErrorDialogListener) getActivity(); activity.onFinishPlayerScoreError(); } }); break; } // This is the line I have removed. // getDialog().setCanceledOnTouchOutside(false); return builder.create(); } 的方法:

FATAL EXCEPTION: main
Process: com.gcapps.richcardsscorekeeper, PID: 5268
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.setCanceledOnTouchOutside(boolean)' on a null object reference
    at com.gcapps.richcardsscorekeeper.ErrorDialog.onCreateDialog(ErrorDialog.java:94)
    at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
    at android.app.BackStackRecord.run(BackStackRecord.java:833)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6134)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是堆栈跟踪:

NullPointerException

关于为什么comp_checklis 仍然被抛出的任何想法?我已经尝试重新启动应用程序,重建项目,使缓存无效并重新启动Android Studio,甚至复制整个类,删除它,并将其粘贴到新文件中(如果旧版本因某种原因而损坏)。

1 个答案:

答案 0 :(得分:0)

您无法从getDialog()内拨打onCreateDialog(),因为您还没有返回警告对话框,让您的DialogFragment知道它是什么。只需在Dialog实例

上调用它即可
Dialog d = builder.create();
d.setCanceledOnTouchOutside(false);
return d;