2013-04-21 92 views
0

我正在編寫客戶端服務器程序,該客戶端將其位置提供給服務器,並由服務器顯示它。 我有一個類來獲取位置的經度和緯度。 我有一個計時器每2秒檢索一次位置。但是,我得到這個錯誤: 不能內螺紋已不叫Looper.prepare創建處理程序()無法創建處理程序... Looper.prepare()

這裏是我的onCreate方法內部定時器:

time.scheduleAtFixedRate(new TimerTask() { 

@Override 
public void run() { 

GetLocation(); 

} 
}, 0, sampling_interval); 
} 

,這裏是getLocation方法:

private void GetLocation(){ 

    gps = new GPSTracker(MainActivity.this);  
    // check if GPS enabled 
    if(gps.canGetLocation()){ 

     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 


     Log.d("Latitude", Double.toString(latitude)); 

    }else{ 

     gps.showSettingsAlert(); 
    } 
} 

問題是: gps = new GPSTracker(MainActivity.this); 如果我把這條線放在MainActivity裏面沒有問題,但問題是我沒有得到更新的位置,我只是得到相同的位置,因爲它只創建一次。所以這行應該在GetLocation()方法裏面,每次定時器調用它時我都可以獲得更新的位置。

任何幫助?

+0

在MainActivity中使用Create Handler並使用handler.post來調用上述方法,因爲要訪問MainActivity之外,需要一個上下文 – Pragnani 2013-04-21 16:23:40

回答

0

這是Android座右銘:避免Timer/TimerTask改爲使用Handler

相關問題