0

我写了这段代码:

package sample;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
import org.freedesktop.dbus.DBusConnection;

@DBusInterfaceName("org.mpris.clementine")
interface Player extends DBusInterface, java.rmi.Remote {
    void Pause();
}


class Main {
        public static void main (String[] args) throws org.freedesktop.dbus.exceptions.DBusException, java.lang.InterruptedException {
                DBusConnection dc = DBusConnection.getConnection(DBusConnection.SESSION);
                Player player = (Player) dc.getRemoteObject("org.mpris.clementine", "/Player");
                player.Pause();
                dc.disconnect();
        }
}

并得到一个错误的行

Player player = (Player) dc.getRemoteObject("org.mpris.clementine", "/Player");

它说

Exception in thread "main" java.lang.ClassCastException: $Proxy2 cannot be cast to sample.Player

我知道问题在于继承误解或类似的东西。我已经彻底搜索过,但找不到答案。如何正确写出这一行?什么是错误?

4

3 回答 3

0

不确定“dc.getRemoteObject()”是做什么的,如果它是远程操作,则需要取消编组该对象。

对象 obj= dc.getRemoteObject("org.mpris.clementine", "/Player");

Player player =(Player)PortableRemoteObject.narrow(obj,Player.class)

于 2013-01-06T15:41:05.133 回答
0

改用这个:

Object obj = dc.getRemoteObject("org.mpris.clementine", "/Player");
if( obj instanceof Player ) {
    Player player = (Player) obj;
    player.pause();
}
于 2013-01-06T14:31:55.773 回答
0

您应该确保返回的类

 dc.getRemoteObject  

实现接口播放器

于 2013-01-06T14:07:17.467 回答