2012-03-20 67 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()因爲生成器被定義爲接受語境