简体   繁体   中英

No suitable method found for addListener(<anonymous ChangeListener<java.time.Duration>>)

I've trouble with the following code. It's showing error while implementing the following code section. It's a code on JavaFX MediaPlayer project.

mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        });

It shows the following error.

C:\Users\User\Documents\NetBeansProjects\Rmedia\src\rmedia\FXMLDocumentController.java:75: error: no suitable method found for addListener(<anonymous ChangeListener<java.time.Duration>>)
        mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
method Observable.addListener(InvalidationListener) is not applicable
  (argument mismatch; <anonymous ChangeListener<java.time.Duration>> cannot be converted to InvalidationListener)
method ObservableValue.addListener(ChangeListener<? super javafx.util.Duration>) is not applicable
  (argument mismatch; <anonymous ChangeListener<java.time.Duration>> cannot be converted to ChangeListener<? super javafx.util.Duration>)

1 error

What should I do to fix this? Thanks in advance.

In order to fix this bug, you need to sort out your imports. You've imported javafx.util.Duration in some classes, and java.time.Duration in other classes.

You need to decide which of these two classes you want to use, and make sure your import statements across all your classes are referring to the same one.

You need to import javafx.util.Duration in your controller class instead of java.time.Duration , similar to what Dawood has mentioned. However, note that there is no "choice" here - JavaFX API uses javafx.util.Duration , and you must follow that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM