简体   繁体   English

方法中的警报对话框 - Android

[英]Alert Dialog in Method - Android

I'm trying to add an alert dialog within a method, but I'm getting an error and I'm not sure why. 我正在尝试在方法中添加警告对话框,但我收到错误,我不知道为什么。 I'm new to Java/Android so it could be something simple. 我是Java / Android的新手,所以它可能很简单。 My following code checks the user's location to make sure it is within a certain area, and if it is, it will start to track the user. 我的以下代码检查用户的位置以确保它在某个区域内,如果是,它将开始跟踪用户。 If it is not within the defined location, I want an alert dialog to pop up to notify the user that they will not be tracked. 如果它不在定义的位置,我想要弹出一个警告对话框,通知用户不会跟踪它们。 I get the error The constructor AlertDialog.Builder(new LocationListener(){}) is undefined on the line specified below. 我收到错误The constructor AlertDialog.Builder(new LocationListener(){}) is undefined在下面指定的行上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();
            }

        }

If anyone has an idea of what I'm doing wrong and how I can fix it, I'd appreciate the help. 如果有人知道我做错了什么以及如何解决它,我会很感激帮助。 Thanks 谢谢

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

or 要么

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

Because this reference your LocationListener ,Not your class Object 因为this引用了您的LocationListener ,而不是您的类Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM