我在Android應用程序中正確顯示警報時出現問題。目標是將GPS座標發送到數據庫,但首先檢查用戶是否在定義的區域。如果用戶不在該地區,我想顯示警報。我不斷收到問題AlertDialog.Builder alert = new AlertDialog.Builder(this);
與錯誤The constructor AlertDialog.Builder(new LocationListener(){}) is undefined
我曾問過一個類似的問題,但解決方案沒有工作,我認爲這是因爲我沒有包含足夠的代碼。我會很感激任何建議!Android應用程序中的警報對話框錯誤
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);
}
這僅僅是一個類,而不是一個活動。我已經嘗試Findlocation.this,FindLocation.SendLocation等各種組合,但我沒有運氣。還有其他建議嗎? – mkyong 2012-03-20 14:50:00
@Alex我更新了我的答案。 – Luksprog 2012-03-20 15:04:15
它的工作!謝謝你的幫助。 – mkyong 2012-03-20 15:15:24