我在我的Android应用程序中正确显示警报时遇到问题。目标是将GPS坐标发送到数据库,但首先检查用户是否在定义的区域中。如果用户在该区域之外,我希望显示警报。我一直在AlertDialog.Builder alert = new AlertDialog.Builder(this);
行上遇到错误The constructor AlertDialog.Builder(new LocationListener(){}) is undefined
我之前问过一个类似的问题,但是解决方案不起作用,我想这是因为我没有包括足够的代码。如果有任何建议,我将不胜感激!
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);
}
发布于 2012-03-20 14:18:23
您在匿名侦听器LocationListener
中声明了您的AlertDialog
。如果您试图通过使用this
在构造函数中获取对Context
的引用,那么实际上将引用LocationListener
,而不是Activity
的Context
。使用您的活动名称+ this
,如ActivityName.this
(如果您在活动中)或获取对Context
的实际引用。
编辑:
在您的FindLocation
类中创建一个以Context
作为参数的构造函数,如下所示:
//private field in your FindLocation class
Context ctx;
public FindLocation(Context ctx) {
this.ctx = ctx;
}
然后,在实例化FindLocation
类的Activity
中,只需传递this
(或ActivityName.this
)
FindLocation obj = new FindLocation(this);
然后,您可以使用字段ctx
来传入AlertDialog
的构造函数。
发布于 2012-03-20 14:16:52
根据您的需要使用这段代码来实现警告框。
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();
}
这是我的代码,所以没有改变任何东西,你可以根据你的需要进行修改。谢谢
发布于 2012-03-20 14:18:43
AlertDialog.Builder alert = new AlertDialog.Builder(this);
您在内部对象 LocationListener(){.....}中调用了此函数。我们知道这个表示的是当前对象,所以它会给出错误。
将此生成器的参数更改为getApplicationContext(),因为构建器被定义为接受Context
https://stackoverflow.com/questions/9788493
复制