asynctask中的Android setText [英] Android setText in asynctask
问题描述
我正在使用UDP程序,但是在asynctask
中应用setText
时遇到了麻烦.基本上在UDP服务器上,我只要求输入一个端口,然后该服务器应连接到localhost和该端口.布局中间的TextView
指示当前未连接",当我单击连接到端口"时,我希望它更改为已连接".但是,它什么也没做.我尝试四处搜寻,但无法提出解决方案.如果有人可以帮助我解决这个问题,将不胜感激.
I'm working on a UDP program but I'm having trouble with applying setText
in my asynctask
. Basically on the UDP server, I just ask to input a port and then the server should connect to the localhost and the port. A TextView
in the middle of the layout says "Currently not connected" and when I click connect to a port, I want it to change to "Connected." However, it is not doing anything. I tried searching around but was unable to come up with a solution. If anyone can help me figure out the problem that would be greatly appreciated.
我的线程异步任务代码:
将setText
移出了doinbackground.但是,setText
仍然不起作用.
My Thread asynctask code:
took setText
out of doinbackground. However, setText
still is not functional.
public class ServerThread extends AsyncTask<String, String, String>{
TextView chatbox;
TextView amiconnected;
@Override
protected void onPreExecute(){
chatbox = (TextView) findViewById(R.id.textView2);
amiconnected = (TextView) findViewById(R.id.textView3);
PORT = Integer.parseInt(((EditText) findViewById(R.id.editText1)).getText().toString());
}
@Override
protected String doInBackground(String... arg0) {
//open socket at specified port on the local host
try {
socket = new DatagramSocket(PORT);
//listening
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
text2 = new String("Now you're connected.");
//convert received message to string
text1 = new String(buf, 0, packet.getLength());
//get client address from received packet
//client_addr = packet.getAddress();
//byte[] message = new byte[1024];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text2;
}
@Override
protected void onPostExecute(String text2) {
// TODO Auto-generated method stub
super.onPostExecute(text2);
chatbox.setText(text2);
}
我的整个UDP服务器代码
My entire UDP Server code
package com.example.udpcomm;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Server extends Activity{
int PORT;
DatagramSocket socket;
InetAddress client_addr;
String text1;
String text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
//chatbox = (TextView) findViewById(R.id.textView2);
Button connectButton = (Button) findViewById(R.id.connectserver);
connectButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
new ServerThread().execute();
}
});
}
//parameter string port (converted into int)
//onprogressupdate (inputting string (?) into TextView)
//on postexecute return null (?)
public class ServerThread extends AsyncTask<String, String, String>{
TextView chatbox;
TextView amiconnected;
@Override
protected void onPreExecute(){
chatbox = (TextView) findViewById(R.id.textView2);
amiconnected = (TextView) findViewById(R.id.textView3);
PORT = Integer.parseInt(((EditText) findViewById(R.id.editText1)).getText().toString());
}
@Override
protected String doInBackground(String... arg0) {
//open socket at specified port on the local host
try {
socket = new DatagramSocket(PORT);
//listening
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
text2 = new String("Now you're connected.");
//convert received message to string
text1 = new String(buf, 0, packet.getLength());
//get client address from received packet
//client_addr = packet.getAddress();
//byte[] message = new byte[1024];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text2;
}
@Override
protected void onPostExecute(String text2) {
// TODO Auto-generated method stub
super.onPostExecute(text2);
chatbox.setText(text2);
}
}
}
推荐答案
您需要将UI
操作移至onPreExecute()
或onPostExecute()
方法,因为doInBackground
无法触摸UI
.我建议您从doInBackground
方法返回需要放入TextView
的字符串或值,然后将其应用于onPostExecute()
方法中的TextView
.参考 AsyncTask描述
You need to move your UI
actions to the onPreExecute()
or the onPostExecute()
methods as doInBackground
cannot touch the UI
. I would suggest you return the string or value you need to put in the TextView
from the doInBackground
method and then apply it to the TextView
in the onPostExecute()
method. Reference AsyncTask Description
这篇关于asynctask中的Android setText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!