gpt4 book ai didi

java - Android Studio 错误 : Can't create handler inside thread that has not called Looper. 准备()

转载 作者:行者123 更新时间:2023-12-01 10:00:43 32 4
gpt4 key购买 nike

我试图在 AsyncTask Activity 中每隔 X 秒运行一段代码,但在进入任何打印语句之前它就崩溃了。我不确定它为什么崩溃,但我也发布了我收到的错误。有人有主意吗?太感谢了!

public class AppListener extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... uri) {


final Handler h = new Handler();
final int delay = 5000; //milliseconds

h.postDelayed(new Runnable() {
public void run() {


try {

String msg_received = null;

System.out.println("LISTENING FOR LAST INSTALLED APP");


System.out.println("TRY");

Socket socket = new Socket("85.190.178.23", 5050);

// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());

System.out.println("DataInputStream Started");

// read data that got sent
msg_received = DIS.readUTF();

System.out.println("Message from server" + msg_received);

// Might not want to close socket, or only the first string will be sent and none after
socket.close();


}

catch (Exception e) {
System.out.println("Did not receive string");
}




h.postDelayed(this, delay);
}
}, delay);



String msg_received = null;
return msg_received;


}

}

我的错误

原因:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序

以下是我如何让循环每 X 秒执行一次,而不干扰我的 GUI(如果对其他人有帮助的话):我没有单独的类,而是在我的主要 Activity 中发布了我的代码。应用程序启动

public class MainActivity extends FragmentActivity{



private Thread repeatTaskThread;


// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


// Go into loop that is repeated every X seconds
RepeatTask();
}




private void RepeatTask()
{
repeatTaskThread = new Thread()
{
public void run()
{
while (true)
{

//Post your code here that you want repeated every X seconds
// My "try" and "catch" statements from above got inserted here


try
{
// Sleep for 5 seconds
Thread.sleep(5000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
};
repeatTaskThread.start();

}
}

最佳答案

更新:当您启动 AsyncTasks doInBackground 时,您正在启动一个后台线程。如果您想延迟发布 AsyncTask 的 doInBackground,那么您根本不应该使用 AsyncTask。您应该只需要使用带有 postDelayed 的 Handler,它将为您创建一个后台线程。看起来在您的代码中,您尝试在 AsyncTask 的后台线程中同时启动一个带有处理程序的新线程。

完全摆脱 AsyncTask,并将此代码包含在您的 Activity 中:

final Handler h = new Handler();
final int delay = 5000; //milliseconds

h.postDelayed(new Runnable() {
public void run() {


try {

String msg_received = null;

System.out.println("LISTENING FOR LAST INSTALLED APP");


System.out.println("TRY");

Socket socket = new Socket("85.190.178.23", 5050);

// Get data sent through socket
DataInputStream DIS = new DataInputStream(socket.getInputStream());

System.out.println("DataInputStream Started");

// read data that got sent
msg_received = DIS.readUTF();

System.out.println("Message from server" + msg_received);

// Might not want to close socket, or only the first string will be sent and none after
socket.close();


}

catch (Exception e) {
System.out.println("Did not receive string");
}




h.postDelayed(this, delay);
}
}, delay);



String msg_received = null;
return msg_received;


}

关于java - Android Studio 错误 : Can't create handler inside thread that has not called Looper. 准备(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829555/

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