java - 方法中的警报对话框 - Android

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

        }

如果有人知道我做错了什么以及如何解决它,我将不胜感激。谢谢

最佳答案

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

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

因为 this 引用了您的 LocationListener,而不是您的类 Object

关于java - 方法中的警报对话框 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750457/

相关文章:

android - 移动抽屉导航图标(汉堡包)

java - 使用 GPS 和 XMLVM 的 J2ME 应用程序

java - 如何将 url 中的查询字符串传递的值传递到 spring Controller 中?

Java串行数据事件处理

java - 有没有办法判断一个java程序是否是第一次运行?

android - 如何正确初始化游标?

java - 如果标准输入仅包含一行,则无法读取它

php - 上传到 PHP 服务器时将文件名写入 OutputStream,以便正确命名文件

video - GPS信息在视频帧中丢失

java - android中如何确定位置?