JavaFX拖放导致Java虚拟机外部崩溃

时间:2013-04-15 16:11:06

标签: java drag-and-drop jvm javafx native-code

嗨,我对JavaFXs DragandDrop(DnD)有一个小问题

正确执行DnD,这意味着文件已创建并移至正确的位置。即使我使用现有文件(然后我不创建新文件),该文件也会随内容正确移动。

以下是代码行:

    void setupGestureSource(final Text source) {
    source.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            Vector<File> vec = new Vector<File>();
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);
            File tmpFile = new File("test.txt");
            try {
                tmpFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            vec.add(tmpFile);
            ClipboardContent content = new ClipboardContent();
            content.putFiles(vec);
            db.setContent(content);
            event.consume();
        }
    });
}

但是在event.consume()之后我的JVM崩溃了,程序以错误结束:

java.lang.NullPointerException
at com.sun.javafx.tk.quantum.QuantumToolkit$14.actionPerformed(QuantumToolkit.java:1154)
at com.sun.glass.ui.Clipboard.actionPerformed(Clipboard.java:139)
at com.sun.glass.ui.win.WinDnDClipboard.push(Native Method)
at com.sun.glass.ui.win.WinSystemClipboard.pushToSystem(WinSystemClipboard.java:213)
at com.sun.glass.ui.SystemClipboard.flush(SystemClipboard.java:28)
at com.sun.glass.ui.ClipboardAssistance.flush(ClipboardAssistance.java:34)
at com.sun.javafx.tk.quantum.QuantumClipboard.flush(QuantumClipboard.java:197)
at com.sun.javafx.tk.quantum.QuantumToolkit.startDrag(QuantumToolkit.java:1195)
at javafx.scene.Scene$DnDGesture.dragDetectedProcessed(Scene.java:2652)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2713)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2607)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3344)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_UNCAUGHT_CXX_EXCEPTION (0xe06d7363) at pc=0x74efc41f, pid=6592, tid=6596
#
# JRE version: 7.0_17-b02
# Java VM: Java HotSpot(TM) Client VM (23.7-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [KERNELBASE.dll+0xc41f]
    at 

com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "JavaFX Application Thread" Error: 80de0001 in checkJavaException(env) RaiseException+0x58
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Apfelmaennchen\workspace\asd\hs_err_pid6592.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

2 个答案:

答案 0 :(得分:0)

我已经尝试过你的代码,但对我来说它很有用 也许你(或你的vm)对这个文件夹没有写权限。

也许使用

List<File> list = new ArrayList<>();

而不是

Vector<File> vec = new Vector<File>();

请您发一个完整的代码示例,以便我可以试试这个吗?

答案 1 :(得分:0)

这是完整的代码示例,仅供参考我使用的是jdk 2.17和windows7,问题出现在3台不同的(windows)机器上

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test App");
        Group rootGroup = new Group();
        Scene scene = new Scene(rootGroup);
        Text text = new Text(100,100,"Drag For new file");
        rootGroup.getChildren().add(text);
        setupGestureSource(text);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    void setupGestureSource(final Text source) {
        source.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                List<File> vec = new ArrayList<File>();
                Dragboard db = source.startDragAndDrop(TransferMode.ANY);
                File tmpFile = new File("test.txt");
                try {
                    tmpFile.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                vec.add(tmpFile);
                ClipboardContent content = new ClipboardContent();
                content.putFiles(vec);
                db.setContent(content);
                event.consume();
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}