Java animateCamera,非法状态异常:不在主线程上
我有一个单独的map类,在这个类中我编写了与map活动相关的所有逻辑,因为严格要求将两个map相关的东西分开。现在从主应用程序活动中,我调用如下函数:Java animateCamera,非法状态异常:不在主线程上,java,android,google-maps,Java,Android,Google Maps,我有一个单独的map类,在这个类中我编写了与map活动相关的所有逻辑,因为严格要求将两个map相关的东西分开。现在从主应用程序活动中,我调用如下函数: Timer t = new Timer(); t.scheduleAtFixedRate(new TimerTask() { @Override public void run() { if (mapObj.isLocationCli
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (mapObj.isLocationClientConnected)
Location currentLocation = mapObj.gotoCurrentLocation();
}
}, 0, refreshUserLocationInterval);
在地图课上,我有:
但我得到了这个错误:
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在map类中保持在同一个函数中。我已经将main app中的主上下文作为变量提供给这个类,我可以利用它并使其工作吗?您需要从主线程调用animateCamera
可以使用or或。事实上,animateCamera会在地图上修改UI组件,因此必须在UI线程上完成 编辑 您可以这样做:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable()
{
public void run()
{
//update ui
if (mapObj.isLocationClientConnected)
Location currentLocation = mapObj.gotoCurrentLocation();
}
});
}
}, 0, refreshUserLocationInterval);
计时器在不同的线程上运行,您只能从ui线程更新ui。使用处理程序。我想在指定的时间间隔后继续调用此函数,可以使用这些吗?@Maven yes,任何一个都可以。@Maven yes处理程序都是一个不错的选择。注:RunNuithRead是一种活动方法class@Maven只需确保在主线程中创建处理程序,即不在gotoCurrentLocation内。
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable()
{
public void run()
{
//update ui
if (mapObj.isLocationClientConnected)
Location currentLocation = mapObj.gotoCurrentLocation();
}
});
}
}, 0, refreshUserLocationInterval);