gpt4 book ai didi

java - AsyncTask android异常(无法执行任务: the task has already been executed)

转载 作者:太空狗 更新时间:2023-10-29 16:31:30 26 4
gpt4 key购买 nike

我有 java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once) 尽管我像这样创建了一个新的 AsyncTask 实例

new ClientEngine(ip, port).execute(WindowsEventsEnum.MouseLeftButtonClick);

这是我的代码

    public class ClientEngine extends AsyncTask<WindowsEventsEnum, Void, Void>{
final String ip;
final int port;
DatagramSocket socket;
InetAddress remoteAdress;
DatagramPacket sendingPacket;
final String VOLUMEUP = "01";
final String VOLUMEDOWN = "02";
final String LMBCLICK = "11";
final String RMBCLICK = "12";
final String MMBUP = "21";
final String MMBDOWN = "22";
final String ENDCONNECTION = "ec";


public ClientEngine(String ip, int port) throws SocketException, UnknownHostException {
this.ip = ip;
this.port = port;
socket = new DatagramSocket();
remoteAdress = InetAddress.getByName(ip);
}

public void OpenConnection() throws IOException {


}

public void CloseConnection() throws IOException {

socket.close();
}

public void MouseLeftButtonClick() throws IOException {
byte[] sendDatagram = LMBCLICK.getBytes();
sendingPacket = new DatagramPacket(sendDatagram, sendDatagram.length, remoteAdress, port);
socket.send(sendingPacket);
}

public void MouseRightButtonClick() throws IOException {
byte[] sendDatagram = RMBCLICK.getBytes();
sendingPacket = new DatagramPacket(sendDatagram, sendDatagram.length, remoteAdress, port);
socket.send(sendingPacket);
}

public void SystemVolumeUp() throws IOException {

}

public void SystemVolumeDown() throws IOException {

}

@Override
protected Void doInBackground(WindowsEventsEnum... params) {
switch (params[0]) {
case MouseLeftButtonClick:
try {
MouseLeftButtonClick();
CloseConnection();
} catch (IOException e) {
e.printStackTrace();
}
break;
case MouseRightButtonClick:
try {
MouseRightButtonClick();
CloseConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

}

最佳答案

很抱歉,如果您已经解决了这个问题。但一般来说,每当你遇到这样的异常时,

java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)

检查您是否遵守以下所有提到的规则:

  1. AsyncTask 只能执行一次(如果尝试执行第二次将抛出异常。)

    就像多次启动一个线程是非法的。

It is never legal to start a thread more than once. In particular, athread may not be restarted once it has completed execution.

所以创建一个像 new asyncTask().execute(); 这样的新实例是唯一的选择

  1. onPreExecute()、doInBackground()、onProgressUpdate()、onPostExecute() 不应手动调用。

  2. AsyncTask 类必须在 UI 线程上加载。从 JELLY_BEAN 开始,这是自动完成的。

  3. 任务实例必须在 UI 线程上创建。

  4. execute(Params...) 必须在 UI 线程上调用。

确保您已遵循代码中的所有 5 条规则。

阅读更多:https://developer.android.com/reference/android/os/AsyncTask.html

关于java - AsyncTask android异常(无法执行任务: the task has already been executed),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37148582/

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