您可以看到我在 onPostExecute 中添加了 tvStatus (TextView),然后删除了我的 progressDialog 框。当我调试我的代码时,我可以看到该值是在 tvStatus 中设置的,但它没有显示在屏幕上。
在调用 onPostExecute 函数后,我的 progressDialog 也停止旋转。有谁知道为什么以及如何解决?
这是在 onCreate 方法中设置的:
tvStatus = (TextView) this.findViewById(R.id.tvStatus);
代码:
public class TcpTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
try {
//set up a Connection
Socket s = new Socket("88.26.249.133", TCP_SERVER_PORT);
InputStream inputstream = (s.getInputStream());
DataInputStream in = new DataInputStream(inputstream);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
s.setSoTimeout(20*1000);
//prepare output message
outBuffer[0] = 48;
outBuffer[1] = 51;
outBuffer[2] = 49;
outBuffer[3] = 49;
outBuffer[4] = 0;
outBuffer[5] = 0;
//send output message
out.write(outBuffer);
out.flush();
//To check in logCat
Log.i("TcpTask", "sent: " + outBuffer);
//check # available data
//and use it as byte length
avail = in.available();
byte[] inBuffer = new byte[avail];
//accept server response
while ((nob = in.read(inBuffer)) != -1) {
}
//close stream
in.close();
for (int i = 7; i < avail-7; i += 2) {
lowByte = inBuffer[i];
highByte = inBuffer[i+1];
if (lowByte < 0) {
result = lowByte + 256 + (highByte * 256);
} else {
result = lowByte + (highByte * 256);
}
}
//close connection
s.close();
//To Check in logCat
Log.i("TcpTask", "received: " + inBuffer);
// if the host name could not be resolved into an IP address.
} catch (UnknownHostException e) {
e.printStackTrace();
Log.i("myStatus", "TcpClient: Host name could not be resolved");
// if an error occurs while creating the socket.
} catch (IOException e) {
e.printStackTrace();
Log.i("myStatus", "TcpClient: ERROR");
} finally {
Log.i("TcpTask", "TCPClient: Finished");
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
tvStatus.append(Integer.toString(result) + System.getProperty("line.separator"));
if (myStatus.this.progDialog != null) {
myStatus.this.progDialog.dismiss();
}
}
}