Страницы

Поиск по вопросам

вторник, 21 мая 2019 г.

Ошибка “the task has already been executed (a task can be executed only once)” в AsyncTask

public class MainActivity extends AppCompatActivity {
int a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Button get_string_button = (Button) findViewById(R.id.get_string_button); Button get_screen_button = (Button) findViewById(R.id.get_screen_button); final SenderThread senderThread = new SenderThread();
View.OnClickListener onClickListener = new View.OnClickListener() { String line; Toast toast;
@Override public void onClick(View view) { try { switch (view.getId()){ case R.id.get_string_button: senderThread.execute("getRandomNumber"); line = senderThread.get(); toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG); toast.show(); break; case R.id.get_screen_button: senderThread.execute("getScreen"); line = senderThread.get(); toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG); toast.show(); break; } } catch (InterruptedException e) { Toast toast = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG); toast.show(); } catch (ExecutionException e) { e.printStackTrace(); } }
}; get_string_button.setOnClickListener(onClickListener); get_screen_button.setOnClickListener(onClickListener); }
}
SenderThread
class SenderThread extends AsyncTask {
private Socket socket; InputStream sin; OutputStream sout; DataInputStream in; DataOutputStream out; CloseSocket closeSocket = new CloseSocket();
@Override protected void onPreExecute() { super.onPreExecute(); }
@Override protected String doInBackground(String... params) {
String line; String command = params[0];
try{ socket = new Socket(ip, 6666); sin = socket.getInputStream(); sout = socket.getOutputStream(); in = new DataInputStream(sin); out = new DataOutputStream(sout); if(socket.isConnected()) { out.writeUTF(command); out.flush(); line = in.readUTF(); closeSocket.close(in,out,socket); return line.toString(); }else{ throw new Exception("Socket not alive!"); } }catch(Exception ex) { return ex.toString(); } } }
logcat
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.lordradix.client, PID: 21168 java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:605) at android.os.AsyncTask.execute(AsyncTask.java:560) at com.lordradix.client.MainActivity$1.onClick(MainActivity.java:44) at android.view.View.performClick(View.java:6261) at android.widget.TextView.performClick(TextView.java:11159) at android.view.View$PerformClick.run(View.java:23748) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)


Ответ

Точно не помню, но по моему один экземпляр AsyncTask нельзя использовать дважды. Создайте другой экземпляр для второго клика, так например:
switch (view.getId()){ case R.id.get_string_button: SenderThread senderThread = new SenderThread(); senderThread.execute("getRandomNumber"); line = senderThread.get(); toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG); toast.show(); break; case R.id.get_screen_button: SenderThread senderThread = new SenderThread(); senderThread.execute("getScreen"); line = senderThread.get(); toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG); toast.show(); break; }
Вам компилятор подсказывает:
the task has already been executed (a task can be executed only once)
То есть
задача уже выполнена (задача может быть выполнена только один раз)

Комментариев нет:

Отправить комментарий