我正在尝试实现此模块,其中显示用户在键入消息时正在键入的模块。我是新手,所以如果发现,请忽略愚蠢的错误。我查看了文档,这是我在代码中提出的:
import java.applet.Applet;
public class ChatBoard extends JFrame implements MessageListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
static String username, password;
static XMPPConnection connection;
private JTextField textField;
static JLabel board = new JLabel("<html>");
static Chat chat;
String message;
static String to;
static boolean flag = true;
ChatState status;
public class typingStatus implements ChatStateListener {
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
}
@Override
public void stateChanged(Chat arg0, ChatState arg1) {
// TODO Auto-generated method stub
System.out.println(arg0.getParticipant() + " is " + arg1.name());
}
}
public void sendChat(String msg) {
//send chat code
}
public ChatBoard(String user, String pass) {
setVisible(true);
username = user;
password = pass;
//JFrame Creation code
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
private void changeStatus() {
// TODO Auto-generated method stub
if (textField.getText().isEmpty() == false) {
try {
ChatStateManager.getInstance(connection)
.setCurrentState(ChatState.composing, chat);
} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
ChatStateManager.getInstance(connection)
.setCurrentState(ChatState.inactive, chat);
} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
});
//JFrame creation code
ConnectionConfiguration config = new ConnectionConfiguration(
"prc.p1.im", 5222, "prc.p1.im");
connection = new XMPPConnection(config);
try {
connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Connected. Error :" + e.getMessage());
}
try {
connection.login(username, password);
} catch (XMPPException e) {
flag = false;
textField.setVisible(false);
sendButton.setVisible(false);
info.setText("Invalid Login!");
welcomeLabel.setText("Invalid user!");
}
connection.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(final Chat chat,
final boolean createdLocally) {
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, final Message message) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
String sender = message.getFrom();
if (username.compareTo("shraz") == 0)
sender = "h";
else
sender = "t";
if (message != null
|| message.getBody() != "")
board.setText(board.getText()
+ "<br> <font color=red>"
+ sender
+ " </font>:<font color=black> "
+ message.getBody() + "</font>");
URL url = getClass().getResource(
"resource/ultrakill_ultimate.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
});
}
@Override
public void processMessage(Chat arg0, Message arg1) {
//Displaying message code here
}
}
聊天正在双向进行。但是,在运行此代码时,无论何时输入任何内容,我都会收到以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Arguments cannot be null.
at org.jivesoftware.smackx.ChatStateManager.setCurrentState(ChatStateManager.java:118)
at ChatBoard$2.changeStatus(ChatBoard.java:165)
at ChatBoard$2.insertUpdate(ChatBoard.java:156)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:202)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:749)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:708)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:670)
at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1379)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:876)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1661)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1908)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:752)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1017)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:889)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:717)
at java.awt.Component.dispatchEventImpl(Component.java:4730)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
有关如何实施此模块的任何评论或我正在做的错误?
LINE 165:
ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing, chat);
LINE 156:
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus(); //<-line 156
}
答案 0 :(得分:3)
您是否在应用程序中添加了以下提供程序?
ProviderManager pm = ProviderManager.getInstance();
pm.addExtensionProvider("active","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("composing","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("paused","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("inactive","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("gone","http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
添加这些提供程序后,您将能够阅读这些状态。
我建议您在首次连接远程服务器时添加和初始化这些提供程序。
希望这可能会有所帮助
答案 1 :(得分:2)
变量chat
未初始化,因此为null
。
答案 2 :(得分:0)
MessageEventManager mem = new MessageEventManager(mConnection); mem.sendComposingNotification(mBuddyName,mMsgid);