Androidアプリにアラートを正しく表示する際に問題が発生しています。目的はGPS座標をデータベースに送信することですが、まずユーザーが定義された領域にいるかどうかを確認します。ユーザーがエリア外にいる場合は、アラートを表示します。私は、エラーThe constructor AlertDialog.Builder(new LocationListener(){}) is undefined
とAlertDialog.Builder alert = new AlertDialog.Builder(this);
というエラーで問題を解決し続けています。私は以前同様の質問をしましたが、解決策はうまくいかず、十分なコードを取り込めなかったためです。私はどんな提案もありがとう!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
@Alex私は自分の答えを更新しました。 – Luksprog
それは働いた!ご協力いただきありがとうございます。 – mkyong