【发布时间】:2014-06-23 01:48:57
【问题描述】:
我有单独的地图类,我在其中编写了与地图活动相关的所有逻辑,因为严格要求将与地图相关的东西分开。现在从主应用程序活动中,我正在调用这样的函数:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (mapObj.isLocationClientConnected)
Location currentLocation = mapObj.gotoCurrentLocation();
}
}, 0, refreshUserLocationInterval);
在Map Class 我有:
public Location gotoCurrentLocation() {
currentLocation = mLocationClient.getLastLocation();
LatLng ll = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom(ll, defaultZoom);
gMap.animateCamera(cUpdate);
return currentLocation;
}
但我收到此错误:
06-22 19:56:30.900: E/AndroidRuntime(11413): FATAL EXCEPTION: Timer-0
06-22 19:56:30.900: E/AndroidRuntime(11413): java.lang.IllegalStateException: Not on the main thread
06-22 19:56:30.900: E/AndroidRuntime(11413): at kbh.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at lzd.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at mbi.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at fms.onTransact(SourceFile:92)
06-22 19:56:30.900: E/AndroidRuntime(11413): at android.os.Binder.transact(Binder.java:310)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.animateCamera(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.google.android.gms.maps.GoogleMap.animateCamera(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.mapworlds.mapworlds.MapClass.gotoCurrentLocation(MapClass.java:176)
我想将animateCamera 保留在地图类中的相同函数中。我已经有来自主应用程序的主上下文作为此类中的一个变量,我可以利用它并使其工作吗?
【问题讨论】:
-
您的计时器在不同的线程上运行,您只能从 ui 线程更新 ui。使用
Handler。
标签: java android google-maps