2012-03-20 6 views
0

Androidアプリにアラートを正しく表示する際に問題が発生しています。目的はGPS座標をデータベースに送信することですが、まずユーザーが定義された領域にいるかどうかを確認します。ユーザーがエリア外にいる場合は、アラートを表示します。私は、エラーThe constructor AlertDialog.Builder(new LocationListener(){}) is undefinedAlertDialog.Builder alert = new AlertDialog.Builder(this);というエラーで問題を解決し続けています。私は以前同様の質問をしましたが、解決策はうまくいかず、十分なコードを取り込めなかったためです。私はどんな提案もありがとう!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は参照しません。アクティビティ名+ thisActivityName.this(活動中の場合)のように使用するか、またはContextへの実際の参照を取得してください。

EDIT:あなたのFindLocationクラスで

は、このようなパラメータとしてContextを取るコンストラクタを作る:

//private field in your FindLocation class 
Context ctx; 

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

は、その後、あなたがFindLocationクラスをインスタンス化Activityにだけ(thisを渡しますまたはActivityName.this

FindLocation obj = new FindLocation(this); 

ctxフィールドを使用して、AlertDialogのコンストラクタを渡すことができます。

+0

これは単なるクラスではなく活動であると受け入れるように定義されているため。 Findlocation.this、FindLocation.SendLocationなどを試してみました。あらゆる種類のコンボがありましたが、運がなかったのです。その他の提案はありますか? – mkyong

+0

@Alex私は自分の答えを更新しました。 – Luksprog

+0

それは働いた!ご協力いただきありがとうございます。 – mkyong

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()にビルダーがコンテキスト