[英]Android Studio Error: Can't create handler inside thread that has not called Looper.prepare()
I am trying to run a section of code every X seconds in an AsyncTask Activity and it is crashing before getting into any print statements. 我正在尝试在AsyncTask活动中每X秒运行一段代码,并且在进入任何打印语句之前它都崩溃了。 I am not sure why it is crashing, but I also posted the error I am getting.
我不确定为什么它崩溃了,但是我也发布了我得到的错误。 Anyone have any ideas?
有人有想法么? Thank you so much!
非常感谢!
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;
}
}
MY ERROR 我的错误
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 原因:java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序
Here is how I got my loop to execute every X seconds without interfering with my GUI if it is helpful to anyone else: instead of having a separate class, I just posted my code in my main activity right when my app starts 如果我的循环对其他人有帮助的话,这就是我每隔X秒执行一次循环而不干扰GUI的方式:我没有在我的应用程序启动时就在我的主要活动中发布了代码,而没有单独的类
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();
}
}
Updated: When you're launching AsyncTasks doInBackground, you're launching a background thread. 已更新:启动AsyncTasks doInBackground时,正在启动后台线程。 If you want to post AsyncTask's doInBackground with a delay, then you should not use an AsyncTask at all.
如果要延迟发布AsyncTask的doInBackground,则完全不应使用AsyncTask。 You should only need to use a Handler with postDelayed, which will create a background thread for you.
您只需要使用具有postDelayed的Handler,它将为您创建一个后台线程。 It looks like in your code, you tried launch a new Thread with a Handler while in AsyncTask's background thread.
它看起来像在你的代码,你试图用一个处理程序,而在的AsyncTask的后台线程启动一个新的线程。
Get rid of the AsyncTask altogether, and include this code in your Activity: 完全摆脱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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.