2010-09-08 21 views
0

我在下面放置一個代碼,我創建了一個本地線程,並在最後一個關閉大括號中出現錯誤,任何人都可以爲我整理它。需要找到線程錯誤

Thread dt = new Thread(this){ 
public void run() 
    { 
     Looper.prepare(); 
     GetCurrentLocation loc = new GetCurrentLocation(RestaurantFinder.this); 
     loc.setLocParams(); 

     int counter = 0; 
     String lat = GetCurrentLocation.getCurrentLatitude(); 
     String lon = GetCurrentLocation.getCurrentLongitude(); 

     while (lat == null && lon == null 
       && counter <= 1000) 
     { 
      lat = GetCurrentLocation.getCurrentLatitude(); 
      lon = GetCurrentLocation.getCurrentLongitude(); 
      counter = counter + 1; 
     } 

     System.out.println("The Latitude are:" + lat); 
     System.out.println("The Longitude are:"+ lon); 

     if (lat == null && lon == null) 
    { 
      // another alert for location not found 

      AlertDialog.Builder builder1 = new AlertDialog.Builder(RestaurantFinder.this); 
      builder1.setTitle("Restaurant Finder"); 
      builder1.setMessage("Unable to find the Current Location"); 
      builder1.setPositiveButton("OK",new DialogInterface.OnClickListener() 
      { 

           @Override 
           public void onClick(DialogInterface dialog,int which) 
           { 


            dialog.dismiss(); 

           } 
      }); 

      AlertDialog dialog1 = builder1.create(); 
      dialog1.show(); 

      // dismiss ProgressDialog by Handler 
      pd.dismiss(); 

     } 
     else 
     { 

      weather(); 
      // dismiss ProgressDialog by Handler 
      pd.dismiss(); 
     } 

    } 

});<--(Error:Syntax error on token ")", Delete this token) 

回答

3

如果你正確地縮進你的代碼,這樣的問題就好辦多了現貨。

如果我正確匹配大括號,我認爲應該通過簡單地刪除)字符來解決即時問題(語法錯誤)。

然而,也有一些是真正可疑的匿名線程類實例化this說法:

  1. 如果你想傳遞這樣一個說法,你需要在你對應的構造匿名內部類。
  2. 看起來好像this可能是Runnable的一個實例。如果是這樣,則覆蓋Threadrun方法將失敗通過構造函數傳遞Runnable的目的。
  3. 子類Thread類通常被認爲是一個壞主意。

我覺得你的代碼應該是這樣的:

Thread dt = new Thread(new Runnable() { 
    public void run() { 
     ... 
    } 
}); 

...這或許可以解釋爲什麼你在第一時間曾在那裏額外)

或者,如果this真的是Runnable你可以寫:

Thread dt = new Thread(this); 
+0

太謝謝你了斯蒂芬C,我整理出來,參照你的答案。就是那個讓我陷入泥潭的「這個」。現在完全正確。 – 2010-09-08 05:36:14

1

LOL ......作爲錯誤說:刪除此令牌),所以做這種方式:

Thread dt = new Thread(this){ 
public void run() 
    { 
     Looper.prepare(); 
     GetCurrentLocation loc = new GetCurrentLocation(RestaurantFinder.this); 
     loc.setLocParams(); 

     int counter = 0; 
     String lat = GetCurrentLocation.getCurrentLatitude(); 
     String lon = GetCurrentLocation.getCurrentLongitude(); 

     while (lat == null && lon == null 
       && counter <= 1000) 
     { 
      lat = GetCurrentLocation.getCurrentLatitude(); 
      lon = GetCurrentLocation.getCurrentLongitude(); 
      counter = counter + 1; 
     } 

     System.out.println("The Latitude are:" + lat); 
     System.out.println("The Longitude are:"+ lon); 

     if (lat == null && lon == null) 
    { 
      // another alert for location not found 

      AlertDialog.Builder builder1 = new AlertDialog.Builder(RestaurantFinder.this); 
      builder1.setTitle("Restaurant Finder"); 
      builder1.setMessage("Unable to find the Current Location"); 
      builder1.setPositiveButton("OK",new DialogInterface.OnClickListener() 
      { 

           @Override 
           public void onClick(DialogInterface dialog,int which) 
           { 


            dialog.dismiss(); 

           } 
      }); 

      AlertDialog dialog1 = builder1.create(); 
      dialog1.show(); 

      // dismiss ProgressDialog by Handler 
      pd.dismiss(); 

     } 
     else 
     { 

      weather(); 
      // dismiss ProgressDialog by Handler 
      pd.dismiss(); 
     } 

    } 
}; 
相關問題