2017-02-14 197 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(); 
相关问题