我有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