无法使用首选项编辑器在单独的 Class 中创建 AlertDialog

Unable to create an AlertDialog in separate Class with Preference Editor

我想从 class 打开一个 AlertDialog,因为我经常使用来自不同活动的对话框,我不想重复代码。

虽然我将上下文作为参数传递,但我得到了 BadTokenException: Unable to add window -- token null is not valid; is your activity running?

public void getWarning(Context context) {

        final AlertDialog mAlertDialog = new AlertDialog.Builder(context).create();

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        final SharedPreferences.Editor editor = preferences.edit();

        LayoutInflater inflater = LayoutInflater.from(context);
        View popUp = inflater.inflate(R.layout.warning_game, null);

        CheckBox check = popUp.findViewById(R.id.checkBox);
        check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    editor.putBoolean("check_warning", false);
                    Log.d(TAG, "onCheckedChanged: warnings disabled");
                    editor.apply();
                } else {
                    editor.putBoolean("check_warning", true);
                    Log.d(TAG, "onCheckedChanged: warnings enabled");
                    editor.apply();
                }
            }
        });
        mAlertDialog.setTitle("Title");
        mAlertDialog.setMessage("message");
        mAlertDialog.setView(popUp, 75, 0, 75, 0);
        mAlertDialog.setButton(androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //open another AlertDialog;
                mAlertDialog.dismiss();
            }
        });
        mAlertDialog.setButton(androidx.appcompat.app.AlertDialog.BUTTON_NEGATIVE, "Abbrechen", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mAlertDialog.dismiss();
            }
        });
        mAlertDialog.show();
    }

但我将 AlertDialog 作为参数传递并在 Acitivty 中更新它,它适用于 AlertDialogs 而无需在首选项中使用上下文:

投射自 Activity:

   ButtonDialogs open = new ButtonDialogs(buttons);
   AlertDialog dialog = new AlertDialog.Builder(getContext()).create();
   open.getDialog(dialog);

class中的方法:

public AlertDialog getDialog(final AlertDialog mAlertdialog) { 

   String button_label = button.getText().toString();

        switch (button_label) {
            default:
                return mAlertdialog;
            case "One":
                mAlertdialog.setTitle("One");
                mAlertdialog.setMessage("Text One");
                mAlertdialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mAlertdialog.dismiss();
                    }
                });
                mAlertdialog.show();
                return mAlertdialog;
       }
   }
}

我还尝试将 AlertDialog 作为参数并使用 Context context = mAlertDialog.getContext() 获取上下文,然后我得到了一个 IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

所以我将 androidx.appcompat.app.AlertDialog 更改为 android.app.AlertDialog 并获得 BadTokenException: Unable to add window -- token null is not valid; is your activity running?

我试过这样的回调: 但我又遇到了 BadTokenException。 在单独的 Class 中创建 AlertDialog in show in Activity 也不起作用。

我现在能做什么?

嗯,我用这个成功了:

public class Activity extends AppCompatActivity {
   Context context = this;

   //not important code

   final ButtonDialogs cast = new ButtonDialogs(context);
   cast.getWarning(context);
}

ButtonDialog.class:

public class ButtonDialogs extends DialogFragment {

   private Context context;

   public ButtonDialogs(Context context) {
      this.context = context;
   }

   public void getWarning(final Context context) {
      final AlertDialog mAlertDialog = new AlertDialog.Builder(context).create();
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
      final SharedPreferences.Editor editor = preferences.edit();
      //do stuff
      mAlertDialog.show();
   }
}