2012-01-15 2 views
0

주 화면의 생성자에서 내 메서드를 실행하면 작동하지만 타이머에서 호출하면 오류가 발생합니다.타이머에서 메서드를 실행할 때 illegalstatexception을받습니다.

public void buildDesc(){ 


    try { 


     JSONObject event = array.getJSONObject(currentPage); 
     String title = event.getString("title"); 

     String by = event.getString("by"); 
     String by_name = event.getString("by_name"); 
     String summary = event.getString("summary"); 
     int nid = event.getInt("nid"); 


    vfm.add(new LabelField(title)); 
     System.out.println("The Title:"+title); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


} 

그리고 타이머 :

timer = new Timer();//Create the timer to loop the events every 5 seconds 
     timer.scheduleAtFixedRate(new TimerTask(){ 
      public void run() { 
       currentPage++; 
       if(currentPage > 3){ 
        currentPage = 0;  
       } 
       System.out.println("Page Position:"+pagePosition(currentPage+1)); 
       gallery.setHorizontalScroll(pagePosition(currentPage)); 

       buildDesc(); 

      } 
     }, 0, 10000); 

내가에하지 않으면 나는 UI 변경 내용을 적용 할 수는 없습니다, 아마도,라는 안드로이드 질문을 읽어 여기에

내 방법입니다 UIThread?

답변

1

당신의 직감이 옳다고 생각합니다. 블랙 베리에서는 Ui 스레드에서 어떤 일도해서는 안됩니다. 나는 타이머 스레드가 IllegalStateException을 얻는 이유가 무엇인지에 대해 종료되고 있다고 생각한다. UI 스레드에 대한 빠른 안내는 here에서 찾을 수 있습니다.

시도 :

timer = new Timer();//Create the timer to loop the events every 5 seconds 
timer.scheduleAtFixedRate(new TimerTask(){ 
    public void run() { 
     currentPage++; 
     if(currentPage > 3){ 
      currentPage = 0;  
     } 
     System.out.println("Page Position:"+pagePosition(currentPage+1)); 

     synchronized(UiApplication.getUiApplication().getEventLock())) { 
      // UI Code here 
      gallery.setHorizontalScroll(pagePosition(currentPage)); 

     buildDesc(); 
     } 
    } 
}, 0, 10000); 

참고 : 위의 테스트되지 않은 것입니다.

관련 문제