Проблема ItemizedOverlay на карте Google

Я пытался открыть пользовательское диалоговое окно при нажатии на маркер на карте Google. Я использую этот код для переопределения метода 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"
          />

But i get this exception when tapping on a marker

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

любая помощь, пожалуйста...

Спасибо.


person Manoj    schedule 07.02.2012    source источник


Ответы (2)


Используйте classname.this вместо getApplicationContext, если это простая активность карты

если это на вкладке, используйте getParent()

потому что badtokenexception возникает, когда не используется правильная ссылка на класс.

person Newts    schedule 07.02.2012
comment
Спасибо Ньютс :) именно в этом и была проблема. - person Manoj; 07.02.2012

Изменять

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

to

    LayoutInflater inflater = (LayoutInflater) MyActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
person RobGThai    schedule 07.02.2012