我有两个应该沟通的课程,但事实并非如此。一个叫做Chatter,当一个监听器在Chatter中收到一条消息时,它应该在第二个类中调用一个叫做ClassView的方法。但我不认为该方法被调用。
以下是Chatter的代码:
package instantmessengerplugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
public class Chatter {
public XMPPConnection connection;
public String user;
public ClassView classView;
public Chat chat;
public PacketFilter packetFilter;
public PacketListener listener;
public Chatter(XMPPConnection connection1,String user1, Display dist)
{
connection = connection1;
user = user1;
openChat();
classView = new ClassView(dist,chat);
}
public void openChat()
{
ChatManager cm = connection.getChatManager();
chat = cm.createChat(user, new MessageListener()
{
public void processMessage(Chat chat ,Message message)
{
if(message.getType().equals(Message.Type.chat))
{
//TableItem item = new TableItem(classView.chatViewer,SWT.NONE);
//item.setText("Them: " + message.getBody());
System.out.println(message.getBody());
classView.updateChat(message);
}
}
}
);
}
}
以下是ClassView的代码:
package instantmessengerplugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
public class ClassView {
public Display displayChat;
public Shell shellChat;
final Table chatViewer;
public Chat chat;
public ClassView(Display dist,Chat chat1){
chat = chat1;
displayChat = dist;
shellChat = new Shell(displayChat);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shellChat.setLayout(gridLayout);
Label contact = new Label(shellChat,SWT.NONE);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.horizontalSpan = 2;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
chatViewer = new Table(shellChat,SWT.NONE);
data = new GridData();
data.horizontalSpan = 2;
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
chatViewer.setLayoutData(data);
final Text chatBox = new Text(shellChat,SWT.SINGLE);
data = new GridData();
data.verticalAlignment = GridData.FILL;
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
chatBox.setLayoutData(data);
Button send = new Button(shellChat,SWT.PUSH);
send.setText("Send");
send.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
String s = chatBox.getText();
TableItem item = new TableItem(chatViewer, SWT.NONE);
item.setText("Me: " + s);
try {
chat.sendMessage(s);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
chatBox.setText("");
}
});
shellChat.pack();
shellChat.open();
while(!shellChat.isDisposed())
{
if(!displayChat.readAndDispatch())
{
displayChat.sleep();
}
}
}
public void updateChat(Message message)
{
TableItem item = new TableItem(chatViewer,SWT.NONE);
item.setText("Them: " + message.getBody());
}
}
当调用Chatter中的processMessage方法时,它应该与ClassView通信,它将调用updateChat,然后更改ClassView中的表对象。它似乎没有发生。该表未更改,并且没有抛出异常。我知道聊天工作正常,因为它将消息打印到控制台就好了。
有谁知道为什么updateChat似乎没有被调用?
更新
我在相关代码上运行了调试器。这是执行时调用的调试堆栈:
FutureTask $ Sync.innerRun()行:不可用[局部变量不可用]
FutureTask.run()行:不可用
ThreadPoolExecutor $ Worker.runTask(Runnable)行:不可用
ThreadPoolExecutor $ Worker.run()行:不可用
Thread.run()行:不可用[局部变量不可用]
我不确定这究竟是什么意思。提到线程是让我觉得方法调用没有与另一个线程同步的原因。
答案 0 :(得分:0)
这是推测,但看起来你的updateChat消息只是创建了一个TableItem而没有将它添加到任何表中。
public void updateChat(Message message)
{
TableItem item = new TableItem(chatViewer,SWT.NONE);
item.setText("Them: " + message.getBody());
}
答案 1 :(得分:0)
您应该使用eclipse调试代码(猜测在使用SWT时使用此IDE)。在processMessage
内设置断点并逐步执行代码。然后,您将看到是否调用updateChat
。
答案 2 :(得分:0)
我和Smack有类似的错误,在我的情况下,诀窍是真正的bug是NullPointer异常。这是我的代码:
for(PartnerMessageCallback callback : callbacks) {
callback.processMessage(null, message);
}
从processMessage抛出了NullPointer异常,但我在StackTrace中看到以下内容:
FutureTask $ Sync.innerRun()行:不可用[局部变量不可用]
就我而言,一个简单的try-catch解决了这个问题:
for(PartnerMessageCallback callback : callbacks) {
try {
callback.processMessage(null, message);
} catch (Exception ex) {
ex.printStackTrace();
}
}
我希望它可以帮到你。