【问题标题】:animateCamera, IllegalStateException: Not on the main threadanimateCamera,IllegalStateException:不在主线程上
【发布时间】: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


【解决方案1】:

事实上,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);

【讨论】:

    【解决方案2】:

    你需要从主线程调用animateCamera()

    您可以使用HandlerrunOnUiThread()post() 来执行此操作。

    【讨论】:

    • 我想在指定的时间间隔后继续调用这个函数,我可以用这些来做吗?
    • @Maven 是的,这些都行。
    • @Maven 是的Handler 将是一个不错的选择。 stackoverflow.com/questions/17839419/android-thread-for-a-timer。注意:runOnUiThread是Activity类的方法
    • @Maven 请务必在主线程中创建Handler(即不在gotoCurrentLocation() 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多