2012-03-20 64 views
0

我在Android应用程序中正确显示警报时出现问题。目标是将GPS坐标发送到数据库,但首先检查用户是否在定义的区域。如果用户不在该地区,我想显示警报。我不断收到问题AlertDialog.Builder alert = new AlertDialog.Builder(this);与错误The constructor AlertDialog.Builder(new LocationListener(){}) is undefined我曾问过一个类似的问题,但解决方案没有工作,我认为这是因为我没有包含足够的代码。我会很感激任何建议!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); 
      } 

回答

1

您正在声明您的AlertDialog在匿名听众LocationListener。如果您试图通过使用this获得对构造函数中的Context的引用,那么您实际上会引用LocationListener而不是ActivityContext。使用您的活动名称+ this,比如ActivityName.this(如果您正在进行某项活动)或获得Context的真实参考。

编辑:

在你FindLocation类做一个构造函数一个Context因为这样的参数:在你实例FindLocationActivity

//private field in your FindLocation class 
Context ctx; 

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

然后,只需通过this(或ActivityName.this

FindLocation obj = new FindLocation(this); 

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

+0

这仅仅是一个类,而不是一个活动。我已经尝试Findlocation.this,FindLocation.SendLocation等各种组合,但我没有运气。还有其他建议吗? – mkyong 2012-03-20 14:50:00

+0

@Alex我更新了我的答案。 – Luksprog 2012-03-20 15:04:15

+0

它的工作!谢谢你的帮助。 – mkyong 2012-03-20 15:15:24

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

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

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

你叫这里面对象新LocationListener的(){} .....。而且我们知道这个代表当前对象,所以它给出了这个错误。

变化的参数getApplicationContext()因为生成器被定义为接受语境