我有java.lang.IllegalStateException:无法执行任务:任务已执行(任务只能执行一次)虽然我创建了一个新的的AsyncTask的实例这样AsyncTask android异常(无法执行任务:任务已执行)
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;
}
}
您正在尝试两次执行相同的任务,这是不被容许的AsyncTask。如果仍然卡住,请将代码发布到实际启动任务的位置。 – NoChinDeluxe
它不一样。我创建了一个类 –
的新实例。再一次,错误状态另有说明,所以我们需要查看代码开始两个任务的位置。 – NoChinDeluxe