When I start my app in Android it works fine but when I try to put this app in the background I get the following error:
This is what I have in the MainActivity:
public class MainActivity extends AppCompatActivity...
.
.
@Override
protected void onPause() {
super.onPause();
mTcpClient.stopClient();
mTcpClient = null;
mTcpClient.stopClient();
arrayList.clear();
mAdapter.notifyDataSetChanged();
}
And this is what I have in the TcpClient class:
public class TcpClient {
.
.
public void stopClient() {
statusWIFIX = false;
Log.e( "DEBUG-->", "Coneccion Cerrada: "+ statusWIFIX );
sendMessage(Constants.CLOSED_CONNECTION+": " + Modelox);
// send message that we are closing the connection
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
Can someone tell me how to correct this?
As the title says, there is something null.
And in the onPause method, you null the mTcpClient before using the stopClient, even though you use it twice(stopClient), it only removes the second StopClient.
Greetings.
your error is the following:
Since mTcpClient is null, when you call stopClient() it will throw "nullPointerException" which is an exception denoting the absence of the element you are trying to call.
Greetings and good energy.
//// ADDING//// I also notice that you use the notifydatasetchange(), I suggest that you don't call it in the onPause() call it in the onResume(), this because I assume that you try to refresh the data that is inside of the adapter, but when the app enters onPause it means that you are sending it to the background, but well... it doesn't make sense to refresh the data at this moment, it's better to call it when it returns to view :)