【发布时间】:2015-10-22 00:34:57
【问题描述】:
我今天开始使用 Visual Studio 2015 开发 Android 应用程序,我想知道为什么我不能显示 AlertDialog,因为它会抛出一个关于 Android.Views.WindowManagerBadTokenException 的异常,我看不出我在哪里弄错了..
private void Button_Click(object sender, EventArgs e)
{
((Button)sender).Text = string.Format("{0} clicks!", count++);
try
{
AlertDialog.Builder builder = new AlertDialog.Builder(this.ApplicationContext);
builder.SetTitle("This is a title..");
builder.SetMessage("This is a message..");
AlertDialog ad = builder.Create();
ad.Show();
}
catch (Exception ex)
{ ((Button)sender).Text = ex.Message; }
}
【问题讨论】:
-
this.ApplicationContext为什么?为什么不直接使用this? -
我想我必须发送
ApplicationContext,因为它在构造函数中请求上下文.. -
由于某种原因,alertdialog 请求一个上下文,但只适用于活动上下文(漂亮的设计,android ......)。你需要在这里活动。 (
this可能是一个活动,不是吗?)
标签: android visual-studio-2015