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 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
.
Use this piece of code according to your need for implementing alert box.
public void alertbox(String title, String message,Activity activiy) {
final AlertDialog alertDialog = new AlertDialog.Builder(activiy).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setIcon(R.drawable.dashboard);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
} });
alertDialog.show();
}
This was my code so didn't changed anything, you can change as per your need. Thanks
AlertDialog.Builder alert = new AlertDialog.Builder(this);
you called this inside object new LocationListener(){.....} . And we know that this represents current object, so it gives that error.
change the parameter this to getApplicationContext() because Builder is defined to accept Context
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.