gpt4 book ai didi

异步任务中的Android setText

转载 作者:行者123 更新时间:2023-11-29 14:32:24 26 4
gpt4 key购买 nike

我正在开发 UDP 程序,但在我的 asynctask 中应用 setText 时遇到问题。基本上在 UDP 服务器上,我只要求输入一个端口,然后服务器应该连接到本地主机和端口。布局中间的 TextView 显示“当前未连接”,当我单击连接到端口时,我希望它更改为“已连接”。但是,它没有做任何事情。我尝试四处搜索,但无法提出解决方案。如果有人能帮我解决问题,我将不胜感激。

我的线程异步任务代码:编辑:从 doinbackground 中取出 setText。但是,setText 仍然不起作用。

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 服务器代码

    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 的字符串或值,然后将其应用于 TextView onPostExecute() 方法。引用 AsyncTask Description

关于异步任务中的Android setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17624122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com