0

我正在开发一个 android 应用程序来连接到服务器。我遇到了一些错误问题,完全相同的应用程序在标准 PC 上与 java swing 完美配合。确切的错误是 E/AndroidRuntime(1257): java.lang.ClassCastException: net.rc.packet.incoming.lister.PServerList cannot be cast to net.rc.packet.Packet 这是我的 PServerList 类

package net.rc.packet.incoming.lister;
import java.util.List;
import net.rc.entity.ServerDescription;
import net.rc.packet.incoming.ServerPacket;
public class PServerList implements ServerPacket {
    public List < ServerDescription > server_descriptions;
    public PServerList(List < ServerDescription > server_descriptions) {
        this.server_descriptions = server_descriptions;
    }
    public String toString() {
            StringBuilder string_builder = new StringBuilder();
            string_builder.append("[");
            for (ServerDescription server_description: server_descriptions) {
                string_builder.append(server_description);
                string_builder.append(", ");
            }
            string_builder.append("]");
            return string_builder.toString();
        }
    }
}

这是我的套接字处理器类

package net.rc.socket;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.inject.Inject;
import net.rc.packet.Packet;
import net.rc.socket.processor.PacketProcessor;
public class SocketProcessor { @ Inject
    public SocketProcessor(List < PacketProcessor > all_processors) {
        this.all_processors = all_processors;
    }
    private final List < PacketProcessor > all_processors;
    private final Queue < Byte > buffer = new LinkedBlockingQueue < Byte > ();
    private PacketProcessor active_processor = null;
    public void accept(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            buffer.offer(bytes[i]);
        }
    }

    public boolean isComplete() {
        while (!buffer.isEmpty()) {
            if (active_processor == null) {
                int packet_code = (buffer.peek() - 32) & 0xFF;
                for (PacketProcessor processor: all_processors) {
                    if (processor.handles(packet_code)) {
                        active_processor = processor;
                        break;
                    }
                }
            } else {
                if (active_processor.isSegmentDone() && !active_processor.isComplete()) {
                    active_processor = null;
                    continue;
                }
            }
            if (active_processor.isComplete()) {
                return true;
            }
            active_processor.accept(buffer.poll());
        }
        if (active_processor == null) {
            return false;
        } else {
            return active_processor.isComplete();
        }
    }

    public < T extends Packet > T getPacket() {
        if (!isComplete()) return null;
        T packet = active_processor.getPacket();
        active_processor = null;
        return packet;
    }

    public boolean isEmpty() {
        return buffer.isEmpty();
    }
}

有任何想法吗?我被困住了。这是日志猫

E/AndroidRuntime(1257): java.lang.ClassCastException: net.rc.packet.incoming.lister.PServerList cannot be cast to net.rc.packet.Packet
E/AndroidRuntime(1257):  at net.rc.socket.SocketProcessor.getPacket(SocketProcessor.java:70)
E/AndroidRuntime(1257):  at net.rc.packet.DefaultPacketServer.receive(DefaultPacketServer.java:40)
E/AndroidRuntime(1257):  at net.rc.RemoteControlHandler.run(RemoteControlHandler.java:23)
E/AndroidRuntime(1257):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
E/AndroidRuntime(1257):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
E/AndroidRuntime(1257):  at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

0
public < T extends Packet > T getPacket() {
    if (!isComplete()) return null;
    T packet = active_processor.getPacket();
    active_processor = null;
    return packet;
}

Extends, 这样使用,不代表T就是Packet. 你可以instanceOf改用。

于 2013-04-29T09:51:55.290 回答