2012-02-07 60 views
0

我一直在尝试弹出一个自定义对话框,当点击谷歌地图的标记。我使用此代码覆盖itemizedOverlay calss的onTap()方法。谷歌地图中的ItemizedOverlay问题

protected boolean onTap(int index) { 
      AlertDialog.Builder builder; 
      AlertDialog alertDialog; 

      Context mContext = getApplicationContext(); 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.custom_dialog, 
              (ViewGroup) findViewById(R.id.layout_root)); 

      TextView textName = (TextView) layout.findViewById(R.id.text); 
      textName.setText("Here is the Name"); 

      ImageView image = (ImageView) layout.findViewById(R.id.image); 
      image.setImageResource(R.drawable.ic_launcher); 


      builder = new AlertDialog.Builder(mContext); 
      builder.setView(layout); 

      alertDialog = builder.create(); 
      alertDialog.show(); 

      return super.onTap(index); 
     } 

这是自定义对话框布局xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      > 
<ImageView android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="10dp" 
      /> 
<TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#FFF" 
      /> 

但在标记

02-07 16:46:51.768: E/AndroidRuntime(315): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

这是http://developer.android.com/guide/topics/ui/dialogs.html

任何帮助,请给出确切的例子攻时,我得到这个例外...

谢谢。

回答

0

使用classname.this代替getApplicationContext 如果简单mapactivity

如果在选项卡,然后使用的getParent()

因为badtokenexception发生时,其不采取正确的类参考。

+0

感谢蝾螈:)恰好是问题。 – Manoj 2012-02-07 12:14:50

0

变化

Context mContext = getApplicationContext(); 
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 

LayoutInflater inflater = (LayoutInflater) MyActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE); 
+0

谢谢RobGThai .. – Manoj 2012-02-07 12:16:18