2012-08-17 71 views
0

我正在構建一個使用GPS來確定用戶當前所在國家/地區的應用程序。現在,由於GPS需要一段時間才能找到位置,而它正在搜索我想顯示ProgressDialog。Android ProgressDialog惱人的錯誤

所以我LocationListener的裏面,我叫了ProgressDialog,我聲明之前稱爲

pd 

,正下方它顯示我創建一個線程做GPS的東西的一部分。出於某種原因,該ProgressDialog沒有顯示:/

這裏是我的代碼:

pd = ProgressDialog.show(TipCalculatorActivity.this, "", "Looking for GPS satellites..."); 
     new Thread() 
     { 
      public void run() 
      { 
      try 
       { 
       Geocoder geocoder = new Geocoder(TipCalculatorActivity.this, Locale.getDefault()); 
       List<Address> addresses; 
       try { 
        addresses = geocoder.getFromLocation(lat, lng, 1); 
        countryCode = addresses.get(0).getAddressLine(2); 
       }catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
       if(countryCode==null){ 
        Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
       Toast.makeText(TipCalculatorActivity.this, countryCode, Toast.LENGTH_LONG).show(); 

       } 
      catch (Exception e) 
      { 
       Log.e("tag",e.getMessage()); 
      } 
     // dismiss the progressdialog 
      pd.dismiss(); 
     } 
     }.start(); 

這裏有什麼問題?

謝謝!

編輯

我的新代碼: 哪裏我稱之爲的AsyncTask:

LocationListener onLocationChange=new LocationListener() { 
    public void onLocationChanged(Location loc) { 
     //sets and displays the lat/long when a location is provided 
     lat = loc.getLatitude(); 
     lng = loc.getLongitude(); 

     MyAsyncTask task = new MyAsyncTask(TipCalculatorActivity.this, lng, lat); 
     task.execute(); 
     Toast.makeText(TipCalculatorActivity.this, "Done :D", Toast.LENGTH_LONG).show(); 

    } 

的的AsyncTask:

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{ 

private Activity activity; 
private ProgressDialog progressDialog; 

private double longitude; 
private double latitude; 
private String countryCode; 

public MyAsyncTask(Activity activity, double longitude, double latitude) { 
    super(); 
    this.activity = activity; 
    this.longitude = longitude; 
    this.latitude = latitude; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    ProgressDialog.show(activity, "", "Looking for GPS satellites..."); 
} 

@Override 
protected Result doInBackground(Void... v) { 
    Geocoder geocoder = new Geocoder(activity, Locale.getDefault()); 
    List<Address> addresses; 
    try { 
     addresses = geocoder.getFromLocation(latitude, longitude, 1); 
     countryCode = addresses.get(0).getAddressLine(2); 
    }catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); 
    } 
    if(countryCode==null){ 
     Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show(); 
    } 
    Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show(); 
    return null; 
} 

@Override 
protected void onPostExecute(Result result) { 
    progressDialog.dismiss(); 
    Toast.makeText(activity.getApplicationContext(), "Finished.", 
    Toast.LENGTH_LONG).show(); 
} 
} 
+0

被敬酒立即顯示出來? – nandeesh 2012-08-17 12:46:20

+0

看看下面的答案 – arielschon12 2012-08-17 14:01:15

回答

1

如果我是你,我將使用的AsyncTask的即:

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{ 

         private Activity activity; 
         private ProgressDialog progressDialog; 

         private long longitude; 
         private long latitute; 

      public MyAsyncTask(Activity activity, Long longitude, Long latitude) { 
          super(); 
       this.activity = activity; 
       this.longitude = longitude; 
       this.latitude = latitude; 
      } 

      @Override 
      protected void onPreExecute() { 
      super.onPreExecute(); 
       progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true); 
      } 

      @Override 
      protected Result doInBackground(Void... v) { 
      //do your stuff here 
      return null; 

      } 

      @Override 
      protected void onPostExecute(Result result) { 
       progressDialog.dismiss(); 
       Toast.makeText(activity.getApplicationContext(), "Finished.", 
         Toast.LENGTH_LONG).show(); 
      } 


} 

從活動稱之爲:

MyAsyncTask task = new AsyncTask(myActivity.this, lng, lat); 
task.execute(); 
+0

我可以像經典的變量一樣傳遞數據給這個AsyncTask嗎? – arielschon12 2012-08-17 12:48:25

+0

是的!根據需要修改AsynTask的構造函數。我編輯了! – 2012-08-17 12:49:17

+0

我可以發回數據嗎?例如我的結果? – arielschon12 2012-08-17 12:58:27