Android应用中的警报对话错误

时间:2012-03-20 14:13:05

标签: java android alertdialog android-context

我在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);
            }

3 个答案:

答案 0 :(得分:1)

您在匿名听众AlertDialog中声明了LocationListener。如果您尝试使用Context来引用构造函数中的this,那么您实际上会引用LocationListener而不是Context Activity }。使用您的活动名称+ this,例如ActivityName.this(如果您参加活动)或获得Context的真实参考。

修改

FindLocation类中创建一个构造函数,将Context作为参数,如下所示:

//private field in your FindLocation class
Context ctx;

public FindLocation(Context ctx) {
     this.ctx = ctx;
}

然后在实例化Activity课程的FindLocation中,只需传递this(或ActivityName.this

FindLocation obj = new FindLocation(this);

然后您可以使用字段ctx传递AlertDialog的构造函数。

答案 1 :(得分:0)

根据您实施警报框的需要使用这段代码。

    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();
 }

这是我的代码所以没有改变任何东西,你可以根据需要改变。感谢

答案 2 :(得分:0)

AlertDialog.Builder alert = new AlertDialog.Builder(this);

你在对象 new LocationListener(){.....} 中调用了它。我们知道 this 代表当前对象,因此它会产生错误。

将参数更改为 getApplicationContext(),因为构建器定义为接受上下文