Hello! I decided to do one resource-intensive operation (the method is in a different class) in another thread:
//MainActivity public void open_generator(int s) { //тут был код, который не имеет отношения к вопросу time_start = System.currentTimeMillis(); Generator.generate(s); }
//Generator public static void generate(final int r) { new Thread(new Runnable() { @Override public void run() { //очень много кода handler.post(new Runnbale() { //вот тут как бы идёт возвращение в основной поток @Override public void run() { MainActivity ma = new MainActivity(); ma.finished(System.currentTimeMillis()); } }); } }).start(); }
//и снова MainActivity public void finished(long at) { time_finish = at; int time = (int)(time_finish - time_start) / 1000; Toast.makeText(MainActivity.this, "Генерация завершена за " + time + " секунд", Toast.LENGTH_LONG).show(); }
After running this code, the application crashes, in the logs a complete nonsense (swears at ContextWrapper
, called from Toast.makeText(...).show();
). I understand that I incorrectly "returned" to the main thread. Even runOnUIThread()
didn't help. What to do?
On the pros do not skimpy.