未选中从对象到Java 7的强制转换
我现在不知道Java不明白错误在哪里。使用Java7未选中从对象到Java 7的强制转换,java,eclipse,list,java-7,Java,Eclipse,List,Java 7,我现在不知道Java不明白错误在哪里。使用Java7 public void chatMessage(String userName, String message) { IScope scope = Red5.getConnectionLocal().getScope(); ISharedObject chat = getSharedObject(scope, "chat"); List<ChatHistoryItem> history = (L
public void chatMessage(String userName, String message) {
IScope scope = Red5.getConnectionLocal().getScope();
ISharedObject chat = getSharedObject(scope, "chat");
List<ChatHistoryItem> history = (List<ChatHistoryItem>) chat.getAttribute("remoteHistory");
ChatHistoryItem item = new ChatHistoryItem();
item.user = userName;
item.date = new Date();
item.message = message;
history.add(item);
chat.setAttribute("remoteHistory", history);
}
public void聊天室消息(字符串用户名,字符串消息){
IScope scope=Red5.getConnectionLocal().getScope();
ISharedObject chat=getSharedObject(范围,“chat”);
List history=(List)chat.getAttribute(“remoteHistory”);
ChatHistoryItem项=新建ChatHistoryItem();
item.user=用户名;
item.date=新日期();
item.message=消息;
历史。添加(项目);
setAttribute(“远程历史记录”,历史记录);
}
错误:
未选中从对象到列表的强制转换
此问题是因为您无法安全地确定将由IsSharedObject.getAttribute方法调用返回的类型。发生在这一行:
List<ChatHistoryItem> history = (List<ChatHistoryItem>) chat.getAttribute("remoteHistory");
List history=(List)chat.getAttribute(“remoteHistory”);
如果此方法返回的对象
不是List
类型,您将收到ClassCastException
。如果该方法返回适当的类型,代码仍将执行
我假设这不是一个破坏代码的错误,它只是一个来自IDE的警告