2017-02-14 123 views
0

我正在使用Sikuli與Java創建一個小型自動化工具。我遇到此未處理的異常錯誤。我試圖將我創建的方法傳遞給GUI actionPerformed方法。未處理的異常類型Java

package mission; 
import org.sikuli.script.FindFailed; 
import org.sikuli.script.Pattern; 
import org.sikuli.script.Screen; 

import tools.ChooseMission; 

public class Story { 

    public static void runStoryMissions(int chapter, int stage) throws FindFailed, InterruptedException { 
     Screen screen = new Screen(); 
     ChooseMission.ChooseChapter(screen,chapter); 
     ChooseMission.ChooseStage(screen, stage); 
     int dailyBiometricCount = ChooseMission.dailyBiometricCount(screen); 

     Pattern start = new Pattern("img/chapters/start.png"); 
     Pattern replay = new Pattern("img/chapters/replay.png"); 
     Pattern next = new Pattern("img/chapters/next.png"); 
     Pattern mission_finish_bar = new Pattern("img/chapters/mission_finish_bar.png"); 


     screen.click(start); 
     System.out.println("The mission has started."); 


     Thread.sleep(2000); 
     while (find(screen, mission_finish_bar) == false) { 

      System.out.println("Still playing the mission..."); 

     } 
     if (screen.exists(mission_finish_bar) != null){ 
      System.out.println("The mission has finished."); 

     } 
     System.out.println("Wait for 5 Seconds"); 
     Thread.sleep(5000); 
     System.out.println("Click repeat button"); 
     screen.click(replay); 

    } 

這裏是我的actionPerformed監聽器按鈕的代碼:

btnStartMissions.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      int chapter = Integer.parseInt(txtFldChapter.getText()); 
      int stage = Integer.parseInt(txtFldStage.getText()); 

      System.out.println("Chapter #: " + chapter); 
      System.out.println("Stage #: " + stage); 


      try { 
       Story.runStoryMissions(chapter, stage); 
      } catch (FindFailed e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 

有一個在故事一個錯誤。 runStoryMissions(章階段),它說:未處理的異常類型FindFailed和InterruptedException的

堆棧跟蹤:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError 
    at mission.Story.runStoryMissions(Story.java:12) 
    at GuiFrame1$2.actionPerformed(GuiFrame1.java:94) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread 
    at java.awt.Robot.checkNotDispatchThread(Unknown Source) 
    at java.awt.Robot.waitForIdle(Unknown Source) 
    at org.sikuli.script.Mouse.move(Mouse.java:345) 
    at org.sikuli.script.Mouse.move(Mouse.java:318) 
    at org.sikuli.script.Mouse.init(Mouse.java:59) 
    at org.sikuli.script.Screen.initScreens(Screen.java:89) 
    at org.sikuli.script.Screen.<clinit>(Screen.java:58) 
    ... 38 more 
+0

你能後的堆棧跟蹤? – paper1111

+1

對我來說很好。 – shmosel

+0

好吧,我發佈了堆棧跟蹤。我注意到,如果我在其他類中調用此方法,那很好。當我嘗試將它附加到GUI按鈕的actionPerformed方法 – user3010500

回答

1

好吧,我找到了答案,我的問題,如果有人有興趣。

  • Sikuli使用java.awt特性,因此腳本無法實現和使用Swing元素。
  • Java AWT機器人動作不能從Java swing容器調用。

解決方案是創建一個新的主題:

 new Thread(() -> { 
      try { 
       Story.runStoryMissions(chapter, stage); 
      } catch (FindFailed | InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     }).start();