Getting java.lang.IllegalStateException: the current thread must have a looper

I get this error and my application crashes:

java.lang.IllegalStateException: The current thread must have a looper!

I didn’t really understand how to use looper on Google, I use streams (mainly for the sleep function), a handler (for loading the image when the Async task starts) and Async (for getting JSON data from the URL). I do not know how to solve this problem, so any suggestions would be helpful.

This is the code for the thread that executes when the button is clicked:

View view = flingContainer.getSelectedView(); view.findViewById(R.id.item_swipe_right_indicator).setAlpha((float) 1.0); Thread timer = new Thread() { public void run() { try { sleep(320); } catch (InterruptedException e) { e.printStackTrace(); } finally { flingContainer.getTopCardListener().selectLeft(); } } }; timer.start(); 

I use this libray and log-cat: image

where: at com.enormous.quotesgram.MainActivity$3.run(MainActivity.java:479) in the last in log-cat corresponds to the line: flingContainer.getTopCardListener().selectLeft(); in the above code snippet.

+6
source share
1 answer

Try the following (unfortunately, I cannot verify the code):

 Thread timer = new Thread() { public void run() { try { sleep(320); } catch (InterruptedException e) { e.printStackTrace(); } finally { runOnUiThread(new Runnable() { @Override public void run() { flingContainer.getTopCardListener().selectLeft(); } }); } } }; 

The idea is that the Timer stream is not a Looper stream (as a result, the exception "Current stream must have a looper"). However, the user interface thread is a Looper thread (see, for example, this site ).

Since flingContainer.getTopCardListener().selectLeft() is probably intended to run on a user interface thread, it fails if it is not called on the side of the pipelined stream.

+1
source

Source: https://habr.com/ru/post/985284/


All Articles