单击标记时不显示AlertDialog

时间:2014-04-26 11:07:38

标签: android google-maps google-maps-markers

*我创建了警告对话框,点击标记后显示 *但是当我点击标记时,它会使应用程序崩溃 *可能是什么问题呢?我有什么遗失的吗? 在此先感谢您

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public boolean onMarkerClick(Marker marker) {

                showAlertDialog();

                return false;

            }


        });



private void showAlertDialog() {

    AlertDialog alert = new AlertDialog.Builder(
            getBaseContext()).create();

    alert.setTitle("Location Selected");
    alert.setMessage("Add this Location to your");
    alert.setButton("Places",

            new DialogInterface.OnClickListener() {

                //code goes here
            });
    alert.setButton("Activities",
            new DialogInterface.OnClickListener() {

                //code goes here
                }

            });
    alert.show();

}

2 个答案:

答案 0 :(得分:0)

尝试

AlertDialog.Builder alert = new AlertDialog.Builder(getBaseContext()).create();

而不是

AlertDialog alert = new AlertDialog.Builder(getBaseContext()).create();

答案 1 :(得分:0)

@SuppressWarnings("deprecation")
    void showAlertDialog(final LatLng markerPosition) {

        AlertDialog alertDialog = new AlertDialog.Builder(GoogleMapViewer.this)
                .create();

        alertDialog.setTitle("Location Selected");

        alertDialog.setMessage("Add this Location to your");

        alertDialog.setButton2("Places", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),
                        "You clicked on Places", Toast.LENGTH_SHORT).show();
            }
        });

        alertDialog.setButton3("Activities",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent act_intent = new Intent();
                        act_intent.setClass(GoogleMapViewer.this,
                                addActivities.class);
                        act_intent.putExtra("username", savedUserName);
                        act_intent
                                .putExtra("latitude", markerPosition.latitude);
                        act_intent.putExtra("longitude",
                                markerPosition.longitude);
                        startActivity(act_intent);

                    }
                });

        alertDialog.show();
    }