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);
}
You are declaring your
AlertDialogin the anonymous listenerLocationListener. If you are trying to get a reference to theContextin the constructor by usingthisyou’ll actually reference theLocationListenerand not theContextof theActivity. Use your Activity name +thislikeActivityName.this(if you are in an activity) or get a real reference to aContext.EDIT:
In your
FindLocationclass make a constructor that takes aContextas a parameter like this:Then in the
Activitywhere you instantiate theFindLocationclass just passthis(orActivityName.this)Then you could use the field
ctxto pass in the constructor of theAlertDialog.