2014-10-27 105 views
0

我正在使用JavaFX突出顯示webview中的文本。但是當我運行我的代碼時,以下例外發生。請有人給我一個解決方案。執行webemgine.executescript時java.lang.reflect.InvocationTargetException

在此先感謝。 編輯:我已經添加了以下錯誤的完整堆棧。正如我所建議的那樣。

@FXML 
private void _do_highlight(WebEngine engine, String _text) { 
    engine.executeScript("$('body').removeHighlight().highlight('" + _text + "')"); 
}  
@FXML 
private void hcone(MouseEvent event) { 
    str = one.getText(); // 'one' is a Textfield ... 
    if (submited == true) {  
     WebEngine engine = webview.getEngine(); 
     engine.loadContent("<body><div id='content'>Hello asdfasdfasdf</div></body>"); 
     _do_highlight(engine, str); // Each time str is found in webview, it must be highlighted 
    } 
} 

此錯誤時

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3437) 
at javafx.scene.Scene$ClickGenerator.access$7900(Scene.java:3365) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3733) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348) 
at  com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273) 
at java.security.AccessController.doPrivileged(Native Method) 
at    com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:553) 
at com.sun.glass.ui.View.notifyMouse(View.java:925) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102) 
at com.sun.glass.ui.win.WinApplication$$Lambda$37/584634336.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757) 
... 30 more 
Caused by: netscape.javascript.JSException: ReferenceError: Can't find variable: $ 
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128) 
at com.sun.webkit.WebPage.twkExecuteScript(Native Method) 
at com.sun.webkit.WebPage.executeScript(WebPage.java:1426) 
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:948) 
at bioassignment.StringOccurenceController._a_highlight(StringOccurenceController.java:83) 
at bioassignment.StringOccurenceController.hcone(StringOccurenceController.java:96) 
... 40 more 
+0

似乎'_do_highlight()'不應該有'@ FXML'註釋,如果它是你從你的控制器調用的私有方法。此外,您應該顯示完整的異常堆棧,以便我們可以找到錯誤的位置。 – 2014-10-27 09:41:44

+0

我試過刪除@FXML後,結果保持不變。現在給出完整的異常堆棧.. – 2014-10-28 08:42:55

回答

0

基於異常

netscape.javascript.JSException: ReferenceError: Can't find variable: $

看來你是不加載正確的jQuery上。

如果你看看這個answer,你必須先加載jQuery,然後執行你的腳本。

這是爲我工作沒有例外(雖然內容無關突出和腳本不工作...):

@FXML private WebView webView; 

@FXML private TextField one; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
}  

@FXML 
private void hcone(ActionEvent event){ 
    String str = one.getText(); 
    if(!str.isEmpty()){ 
     WebEngine engine = webView.getEngine(); 
     engine.loadContent("<body><div id='content'>Hello asdfasdfasdf</div></body>"); 
     engine.documentProperty().addListener(
      (prop, oldDoc, newDoc) -> _do_highlight(engine, str)); 
    } 
} 

private void _do_highlight(WebEngine engine, String _text) { 
    executejQuery(engine,"$(\"body\").removeHighlight().highlight('" + _text + "');"); 
} 

其中executejQuery()在這個問題的答案,如果是檢查所指向的方法加載到webEngine中的文檔有一個jQuery版本,與所需的最低版本加載相對應,如果不是,則從默認jQuery位置將jQuery加載到文檔中。

請注意,我添加了一個監聽器,因爲該函數需要在執行腳本以注入jQuery之前將文檔加載到WebView中。

相關問題