2013-08-02 2 views
2

Я хотел бы изменить положение карты в Google Maps v2набор карт в Google Maps с TimerTask

Но Ive сделали это в TimerTask ... цель, увеличить масштаб, азимут и так далее, и это говорит

«IllegalStateException -? не в главном потоке

Что я должен сделать какой-либо помощи

class Task extends TimerTask { 

    @Override 
    public void run() { 
     CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(Zt)  // Sets the center of the map to Mountain View 
       .zoom(12)     // Sets the zoom 
       .bearing(180)    // Sets the orientation of the camera to east 
       .tilt(30)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 

     mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
    } 
} 

Timer timer = new Timer(); 

timer.scheduleAtFixedRate(new Task(), 0, 20000); 
+0

Вы не можете обновить интерфейс пользователя за пределами основной/ui-нити. Это может помочь: http://stackoverflow.com/questions/7010951/error-updating-textview-from-timertasks-run-method – Marcelo

+1

Большое спасибо - это работает :) –

ответ

0

задача таймер работает в другом потоке, чем поток пользовательского интерфейса В Android вы. не разрешено выполнять работу пользовательского интерфейса из потока, отличного от UI. Используйте метод runOnUiThread для отправки операций в поток пользовательского интерфейса:

class Task extends TimerTask { 

    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       CameraPosition cameraPosition = new CameraPosition.Builder() 
         .target(Zt)  // Sets the center of the map to Mountain View 
         .zoom(12)     // Sets the zoom 
         .bearing(180)    // Sets the orientation of the camera to east 
         .tilt(30)     // Sets the tilt of the camera to 30 degrees 
         .build();     // Creates a CameraPosition from the builder 

       mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
      } 
     }); 
    } 
} 

Timer timer = new Timer(); 

timer.scheduleAtFixedRate(new Task(), 0, 20000); 
Смежные вопросы