简体   繁体   English

调用方法时似乎未在运行

[英]Method doesn't seem to be running when it is called

I have two classes that should be communicating but they aren't. 我有两个班级应该进行交流,但不是。 One is called Chatter and when a listener receives a message within Chatter it should call a method within the second class which is called ClassView. 一个称为Chatter,当侦听器在Chatter中接收到消息时,应在第二个类中调用一个称为ClassView的方法。 But I don't think the method is being called. 但我不认为该方法被调用。

Here is the code for Chatter: 这是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);
                }


            }
        }




        );


    }





}

And here is the code for ClassView: 这是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());
    }



}

When the processMessage method in Chatter is called it should speak to ClassView which will call updateChat which then changes the table object in ClassView. 调用Chatter中的processMessage方法时,它应与ClassView对话,后者将调用updateChat,后者随后将更改ClassView中的表对象。 It doesn't seem to be happening. 它似乎没有发生。 The table isn't changed and there isn't an exception thrown. 该表未更改,也没有引发异常。 I know that chatter is working correctly because it prints the message to the console just fine. 我知道chatter工作正常,因为它可以将消息正确打印到控制台。

Does anyone know why updateChat doesn't seem to be getting called? 有谁知道为什么updateChat似乎没有被调用?

Update: 更新:

I have ran the debugger at the code in question. 我已经在有问题的代码上运行了调试器。 Here is the debug stack of what is being called at execution: 这是在执行时调用的调试堆栈:

FutureTask$Sync.innerRun() line: not available [local variables unavailable] FutureTask $ Sync.innerRun()行:不可用[本地变量不可用]

FutureTask.run() line: not available FutureTask.run()行:不可用
ThreadPoolExecutor$Worker.runTask(Runnable) line: not available ThreadPoolExecutor $ Worker.runTask(Runnable)行:不可用
ThreadPoolExecutor$Worker.run() line: not available ThreadPoolExecutor $ Worker.run()行:不可用
Thread.run() line: not available [local variables unavailable] Thread.run()行:不可用[本地变量不可用]

I am not sure what it means exactly. 我不确定这到底意味着什么。 Mention of threads is what made me think it was something to do the method call not being synchronised with another thread. 提到线程是让我认为执行方法调用不与另一个线程同步的原因。

This is speculation, but it appears your updateChat message just creates a TableItem without adding it to any table. 这是推测,但是看来您的updateChat消息只是创建TableItem而不将其添加到任何表中。

    public void updateChat(Message message)
{
    TableItem item = new TableItem(chatViewer,SWT.NONE);
    item.setText("Them: " + message.getBody());
}

You should debug the code with eclipse (guess you use this IDE as you use SWT). 您应该使用eclipse调试代码(猜测您在使用SWT时会使用此IDE)。 Set a breakpoint inside processMessage and step through the code. processMessage内设置一个断点并逐步执行代码。 Then you'll see if updateChat is called or not. 然后,您将查看是否updateChat

I had a similar bug with Smack, in my case the trick was that the real bug was a NullPointer exception. 我在Smack上有一个类似的错误,在我看来,窍门是真正的错误是NullPointer异常。 Here is my code: 这是我的代码:

for(PartnerMessageCallback callback : callbacks) {
   callback.processMessage(null, message);
}

A NullPointer exception has thrown from the processMessage, but I see the following in the StackTrace: processMessage引发了NullPointer异常,但是我在StackTrace中看到以下内容:

FutureTask$Sync.innerRun() line: not available [local variables unavailable] FutureTask $ Sync.innerRun()行:不可用[本地变量不可用]

In my case, a simple try-catch solved the problem: 就我而言,一个简单的try-catch解决了这个问题:

for(PartnerMessageCallback callback : callbacks) {
  try {
    callback.processMessage(null, message);
  } catch (Exception ex) {
    ex.printStackTrace();
  }
}

I hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM