简体   繁体   中英

Alert Dialog in Method - Android

I'm trying to add an alert dialog within a method, but I'm getting an error and I'm not sure why. I'm new to Java/Android so it could be something simple. My following code checks the user's location to make sure it is within a certain area, and if it is, it will start to track the user. If it is not within the defined location, I want an alert dialog to pop up to notify the user that they will not be tracked. I get the error The constructor AlertDialog.Builder(new LocationListener(){}) is undefined on the line specified below.

        locListener = new LocationListener() {
        public void onLocationChanged(Location loc) {

            String lat = String.valueOf(loc.getLatitude()); 
            String lon = String.valueOf(loc.getLongitude());

            Double latitude = loc.getLatitude();
            Double longitude = loc.getLongitude();

            if (latitude >= 39.15296 && longitude >= -86.547546 && latitude <= 39.184901 && longitude <= -86.504288) {
                    Log.i("Test", "Yes");                           
                    CityActivity check = new CityActivity();
                    check.checkDataBase(usr);

                    SendLocation task = new SendLocation();
                    task.execute(lat, lon, usr);
            }
            else {
                Log.i("Test", "No");
                AlertDialog alertDialog = new AlertDialog.Builder(this).create(); //ERROR OCCURS HERE
                alertDialog.setTitle("Reset...");
                alertDialog.setMessage("Are you sure?");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // here you can add functions
                   }
                });
                alertDialog.setIcon(R.drawable.icon);
                alertDialog.show();
            }

        }

If anyone has an idea of what I'm doing wrong and how I can fix it, I'd appreciate the help. Thanks

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

or

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();

Because this reference your LocationListener ,Not your class Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM