I have my application that is using Koush's web-sockets/Socket.IO library for just one fragment. (https://github.com/koush/android-websockets)
I am having some problems getting the Socket.IO client to work correctly with the fragment lifecycle and its threads.
For the most part, my fragment looks like this:
public class ScoresFragment extends SherlockFragment {
public SocketIOClient socket;
@Override
public void onCreate(Bundle savedInstanceState) {
createSocket();
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
try {
this.socket.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
super.onPause();
}
@Override
public void onResume() {
socket.connect();
super.onResume();
}
public void createSocket() {
this.socket = new SocketIOClient(URI.create("http://blah-url"), new SocketIOClient.Handler() {
//Required methods here
}
}
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
//Everything for the layout here
}
}
And the error I'm getting is in the onPause() method when I try to disconnect the socket. The socket connects just fine, but as soon as I change fragments I get a crash.
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.codebutler.android_websockets.SocketIOClient.cleanup(SocketIOClient.java:195)
at com.codebutler.android_websockets.SocketIOClient.disconnect(SocketIOClient.java:188)
at edu.bgsu.asf.athletics.fragments.ScoresFragment.onPause(ScoresFragment.java:14)
I read somewhere that I may have to be wary of threads and I may have to use a handler for this, but the git page for web-sockets doesn't allude to having to use anything like that. Thanks in advance.
EDIT:
The sections of SocketIOClient.java:
public void disconnect() throws IOException {
cleanup(); //Line 188
}
/////////////////////////////////////////////
private void cleanup() {
mClient.disconnect();
mClient = null;
mSendLooper.quit(); //Line 195
mSendLooper = null;
mSendHandler = null;
}