0

这是我的 NearMe 活动。我想在谷歌地图上做标记。然后有人单击标记或覆盖我想显示一个警报对话框。这是我从 MapGroupActivity 调用的 MapActivity

public class NearMeActivity extends MapActivity implements OnClickListener {
    private Button imgbtn_map_filter;
    private MapView mapView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.nearme_layout);

        setMapNOverlay();
        setUI();
        imgbtn_map_filter.setOnClickListener(this);

        final TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.closeAllChildsExceptLastOne();
    }

    private void setUI() {
        imgbtn_map_filter = (Button) findViewById(R.id.btn_map_filter);
    }

    private void setMapNOverlay() {
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.balloon);
        MyItemizedOverlay itemezedOverlay = new MyItemizedOverlay(drawable,
                this);

        GeoPoint point = new GeoPoint(19240000, -99120000);
        OverlayItem overlayitem = new OverlayItem(point, "Holaaaa! !!",
                "Go away");

        itemezedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemezedOverlay);

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_map_filter:
            //start activity for result da var onlar incelenecek
            Intent intent = new Intent(getParent(), TestActivity.class);
            TabGroupActivity parentActivity = (TabGroupActivity) getParent();
            parentActivity.startChildActivity ("TestActivity", intent);
            break;
        default:
            break;
        }

    }
}

这是 ItemizedOverlay 类。我正在尝试显示一个警报对话框。

public class MyItemizedOverlay extends ItemizedOverlay {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;
    public MyItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    //bir Overlay'e dokunduğu zaman olaylar olaylar 
    @Override
    protected boolean onTap(int index) {
        OverlayItem item =mOverlays.get(index);
        AlertDialog.Builder dialog= new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        try{
        dialog.show();
        }catch(Exception el){
           Log.d("Error: ---->" + e.toString)
        }
return super.onTap(index);

}

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        // ne zaman bir overlay eklesek populate methodunu çalıştırmamız
        // gerekmektedir. Yen bir overlay
        // nesnesi eklemek için
        // populate methodu çalıştığı zaman overlay createItem çalıştıracaktır
        populate();
    }

    // eklenen overlay'in yerini verir.
    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    // eklenen yeni overlayın seize'ını almamız gerekmektedir
    @Override
    public int size() {
        return mOverlays.size();

    }
}

当我写 Toast.makeText(mContext,"Bla bla",Toast.LENght_LONG).show() --> 运行成功但警告对话框构建器在点击方法中不起作用。

我正在接受此错误:android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40532be8 无效;您的活动正在运行吗?

返回 super.onTap(index);

4

1 回答 1

1

在 NearMeActivity 中复制此代码,

        @Override
protected Dialog onCreateDialog(int id) {

    // TODO Auto-generated method stub

    switch (id) {
        case 0:
            return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
                    R.drawable.ic_launcher).setPositiveButton("Yes",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog ,
                                int which) {

                            // TODO Auto-generated method stub

                        }
                    }).setCancelable(true).setNegativeButton("Cancel",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog ,
                                int which) {

                            // TODO Auto-generated method stub

                        }
                    })

            .setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {

                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Dismiss",
                            Toast.LENGTH_SHORT).show();
                }

            }).create();


        default:
            break;
    }
    return null;
}

并从 com.google.android.maps.ItemizedOverlay 子类的 onTap 方法调用此方法以显示对话框:

     @Override
    protected boolean onTap(int index) {

        // TODO Auto-generated method stub

        switch (index) {
            case 0:
                showDialog(0);
                                    break;
                     }
            }
于 2012-05-07T12:06:47.810 回答