2012-03-17 13 views
0

メソッド内に警告ダイアログを追加しようとしていますが、エラーが発生しています。理由がわかりません。私はJava/Androidには新しいので、何か簡単かもしれません。私の次のコードでは、ユーザーの場所が特定の範囲内にあることを確認し、そうであればユーザーの追跡を開始します。定義された場所にない場合は、警告ダイアログがポップアップして、追跡されないことをユーザーに通知します。私はエラーThe constructor AlertDialog.Builder(new LocationListener(){}) is undefinedを下記の行に表示します。メソッドの警告ダイアログ - Android

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

     } 

誰かが私が間違っていることを考えていると私はそれを修正する方法については、私は助けていただければ幸いです。おかげ

答えて

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

または

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

あなたLocationListener、ないあなたのクラスObject

+2

なぜ 'YourClassName.this'だけではなく、' this'のthis参照するため? 'AlertDialog.Builder'呼び出しは' LocationListener'の無名サブクラスにあるので、 'this'は' Context'のインスタンスを参照しません。 –

関連する問題