【问题标题】:Context problem in Android example "Google Map View"Android 示例“Google 地图视图”中的上下文问题
【发布时间】:2010-02-12 22:06:40
【问题描述】:

我尝试在 Android 开发者网站上实现 Google Map View 教程,但是当我单击覆盖图像时尝试显示 AlertDialog 时,我一直遇到问题。问题是调用时mContext为null

AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

在HelloItemizedOverlay的onTap方法中因为构造函数

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

永远不会被调用(我可以说)初始化 mContext。当我更换

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, 
                this.getApplicationContext());

在 HelloGoogleMaps 的 onCreate 方法中为了初始化上下文,我得到一个异常

android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序

当我尝试像这样显示 AlertDialog 时:

dialog.show();

我假设本教程已经被成千上万的人成功实施,所以我很困惑为什么没有其他人遇到这个问题...我错过了教程中的重要一步吗?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    我认为您需要像这样将 mapView 的上下文传递给 HelloItemizedOverlay 构造函数:

    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mapView.getContext());
    

    教程的代码示例中显然存在错误。像这样的小错误和遗漏在参考文档中并不少见,尤其是对于像 Android 这样的大型项目。

    【讨论】:

    • “像这样的小错误和遗漏在参考文档中并不少见” - 是的,但不必如此。 ;-)
    • this question 解决了教程中相同的两个错误。
    • 由于我的设置,...(drawable, MyMapViewActivity.this.getParent());为我工作。
    【解决方案2】:

    在构造函数中它似乎应该是:

    super(boundCenterBottom(defaultMarker));
    

    不是:

    super(defaultMarker);
    

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多