方法中的警报对话框 - Android

时间:2012-03-17 13:46:33

标签: java android gps android-alertdialog

我尝试在方法中添加警告对话框,但我收到错误,我不确定原因。我是Java / Android的新手,所以它可能很简单。我的以下代码检查用户的位置以确保它在某个区域内,如果是,它将开始跟踪用户。如果它不在定义的位置,我想要弹出一个警告对话框,通知用户不会跟踪它们。我在下面指定的行上收到错误The constructor AlertDialog.Builder(new LocationListener(){}) is undefined

        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 alertDialog = new AlertDialog.Builder(this).create(); //ERROR OCCURS HERE
                alertDialog.setTitle("Reset...");
                alertDialog.setMessage("Are you sure?");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // here you can add functions
                   }
                });
                alertDialog.setIcon(R.drawable.icon);
                alertDialog.show();
            }

        }

如果有人知道我做错了什么以及如何解决它,我会很感激帮助。感谢

1 个答案:

答案 0 :(得分:2)

 AlertDialog alertDialog = new AlertDialog.Builder(YourClassName.this).create();

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();

由于this引用了您的LocationListener,而不是您的班级Object