我正在开发一个连接到服务器的Android应用程序。 我遇到了一些错误问题,完全相同的应用程序与标准PC上的java swing完美配合。 确切的错误是E / AndroidRuntime(1257):java.lang.ClassCastException:net.rc.packet.incoming.lister.PServerList无法强制转换为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();
}
}
}
这是我的socketprocessor类
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();
}
}
有什么想法吗?我被卡住了。 这是logcat
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)
答案 0 :(得分: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
。