Question

*I have created alert dialog to show after clicking on marker
*but when i click on marker it crashes the application.
*What could be the problem? Is there anything am i missing?
Thanks in Advance

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();

}
Was it helpful?

Solution

@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();
    }

OTHER TIPS

Try

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

instead of

AlertDialog alert = new AlertDialog.Builder(getBaseContext()).create();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top