Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:13:48+00:00 2026-05-31T20:13:48+00:00

I’m having problems properly displaying an alert in my Android app. The goal is

  • 0

I’m having problems properly displaying an alert in my Android app. The goal is to send GPS coordinates to a database, but first check to see if the user is in a defined area. If the user is outside of the area, I want to display the alert. I keep getting problems on the line AlertDialog.Builder alert = new AlertDialog.Builder(this); with the error The constructor AlertDialog.Builder(new LocationListener(){}) is undefined I asked a similar question previously, but the solutions did not work and I think it’s because I failed to include enough code. I’d appreciate any suggestions!

        import java.io.BufferedReader;

        public class FindLocation  {

        protected static final Context SendLocation = null;
        private LocationManager locManager;
        private LocationListener locListener;


        private class SendLocation extends AsyncTask<String, Void, String> {

            protected String doInBackground(String... params) {

                String lat = params[0];
                String lon = params[1];
                String usr_id2 = params[2];

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://example.com/example.php");

                try {
                       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                       nameValuePairs.add(new BasicNameValuePair("lat", lat)); 
                       nameValuePairs.add(new BasicNameValuePair("lon", lon));
                       nameValuePairs.add(new BasicNameValuePair("id", usr_id2));
                       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                       httpclient.execute(httppost);
                 } 
                 catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                 } 
                 catch (IOException e) {
                     // TODO Auto-generated catch block
                 }
                return lon; 
            }
        }
        public void startLocation(Context context, String usr_id2) { //changed context to final when AlertDialog was added

            final String usr = usr_id2;

            //get a reference to the LocationManager
            locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);


            //checked to receive updates from the position
            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.Builder alert = new AlertDialog.Builder(this); //****error here*****
                        alert.setTitle("Warning");
                        alert.setMessage("Sorry");
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int which) {
                              // here you can add functions
                           }
                        });
                        alert.show();
                    }

                }
                public void onProviderDisabled(String provider){
                }
                public void onProviderEnabled(String provider){ 
                }
                public void onStatusChanged(String provider, int status, Bundle extras){
                    }
                };
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locListener);
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:13:49+00:00Added an answer on May 31, 2026 at 8:13 pm

    You are declaring your AlertDialog in the anonymous listener LocationListener. If you are trying to get a reference to the Context in the constructor by using this you’ll actually reference the LocationListener and not the Context of the Activity. Use your Activity name + this like ActivityName.this(if you are in an activity) or get a real reference to a Context.

    EDIT:

    In your FindLocation class make a constructor that takes a Context as a parameter like this:

    //private field in your FindLocation class
    Context ctx;
    
    public FindLocation(Context ctx) {
         this.ctx = ctx;
    }
    

    Then in the Activity where you instantiate the FindLocation class just pass this(or ActivityName.this)

    FindLocation obj = new FindLocation(this);
    

    Then you could use the field ctx to pass in the constructor of the AlertDialog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.