Я хотел бы открыть 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);
метод в классе:
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?
Я пробовал такие обратные вызовы: https://stackoverflow.com/a/23533472/11956040, но я просто снова получаю исключение BadTokenException . Создание AlertDialog в отдельном классе в шоу в действии также не работает.
Что я могу сделать сейчас?