非活动视图中的AlertDialog框

我正在尝试修改现有应用程序以在View类中包含警报对话框。 如果我在一个活动(另一个应用程序)中运行它,警报对话框工作正常。 但是,当我将它添加到现有应用程序中的View类时,它会失败并显示(无法添加窗口 – 令牌null)错误。 我尝试了“this”(当前View),getContext()和getApplicationContext()。 我读到并非所有的视图都附加到一个活动,因此getContext失败。 有没有其他选择? 我可以使用某种不依赖于活动的通用警报对话框吗?

Dialog dialog; final ArrayList itemsSelected = new ArrayList(); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Selection:"); builder.setMultiChoiceItems(items.toArray(new String[items.size()]), null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int selectedItemId, boolean isSelected) { if (isSelected) { itemsSelected.add(selectedItemId); } else if (itemsSelected.contains(selectedItemId)) { itemsSelected.remove(Integer.valueOf(selectedItemId)); } } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // OK } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // Cancel } }); dialog = builder.create(); dialog.show(); 

不,但您可以将Activity用作一种代理。 这是一个示例类(在Kotlin中):

 /** * Activity used solely for showing a dialog outside of an activity context */ class DialogActivity : AppCompatActivity() { companion object { const val EXTRA_TITLE = "title" const val EXTRA_MESSAGE = "message" const val EXTRA_YES_RES = "yes_res" const val EXTRA_NO_RES = "no_res" var yesAct: DialogInterface.OnClickListener? = null var noAct: DialogInterface.OnClickListener? = null } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) try { val title = intent.getIntExtra(EXTRA_TITLE, android.R.string.untitled) val message = intent.getIntExtra(EXTRA_MESSAGE, android.R.string.untitled) val yesRes = intent.getIntExtra(EXTRA_YES_RES, 0) val noRes = intent.getIntExtra(EXTRA_NO_RES, 0) val builder = AlertDialog.Builder(this) builder.setTitle(title) builder.setMessage(message) if (yesRes != 0) builder.setPositiveButton(yesRes, yesAct) if (noRes != 0) builder.setNegativeButton(noRes, noAct) builder.setOnCancelListener { finish() } builder.setOnDismissListener { finish() } builder.show() } catch (e: Exception) { e.printStackTrace() finish() } } override fun onDestroy() { super.onDestroy() yesAct = null noAct = null } /** * Builder class * Create a DialogActivity instance using this */ class Builder(private val context: Context) { var title = android.R.string.untitled var message = android.R.string.untitled var yesRes = android.R.string.yes var noRes = android.R.string.no var yesAction: DialogInterface.OnClickListener? = null var noAction: DialogInterface.OnClickListener? = null fun start() { val intent = Intent(context, DialogActivity::class.java) intent.putExtra(EXTRA_TITLE, title) intent.putExtra(EXTRA_MESSAGE, message) intent.putExtra(EXTRA_YES_RES, yesRes) intent.putExtra(EXTRA_NO_RES, noRes) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) yesAct = yesAction noAct = noAction context.startActivity(intent) } } } 

要使用它,请使用

 DialogActivity.Builder builder = new DialogActivity.Builder(context); builder.title = whatever; builder.message = whatever; //etc builder.start(); 

如果您需要为yes / no操作设置侦听器,遗憾的是,我找不到任何其他方法,只能通过静态变量yesActnoAct来完成。

使用此作为活动的主题: