2011-07-14 45 views
0

我试图显示一个AlertDialog,但当我调用show函数时出现错误。我用下面的代码,从代码Hello Mapview样品复制:“AlertDialog”问题

AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    dialog.setTitle(article.getTitle()); 
    dialog.setMessage(article.getSnippet()); 
    dialog.show(); 

应用程序崩溃正在执行中的最后一行时。我怀疑context变量,但它没有内疚,因为有了它,我可以显示Toast

在此先感谢您花费时间来帮助我。

+0

如果可能,请在此处发布错误日志。 – Chandan

回答

1

二@Brigham。确保您传递给ItemizedOverylay的上下文是显示MapView的活动。换句话说,使用类似下面

itemizedOverlay = new HelloItemizedOverlay(drawable, this); 

而不是

itemizedOverlay = new HelloItemizedOverlay(drawable, getApplicationContext()); 

应用程序上下文不能用于AlertDialog,并会导致以下错误,

ERROR/AndroidRuntime(8679):android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记null不适用于应用程序

0

您需要在创建新的AlertDialog.Builder对象后调用.create()。

0

我想一切都会正常工作。在下面的代码片断

AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    dialog.setTitle(article.getTitle()); 
    dialog.setMessage(article.getSnippet()); 
    dialog.show(); 

进行此更改

AlertDialog dialog = new AlertDialog.Builder(context); 
    dialog.setTitle(article.getTitle()); 
    dialog.setMessage(article.getSnippet()); 
    dialog.show();