5

I am getting this error, and my application crashes:

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

I didn't get much about how to use looper on Google, I am using threads(mainly for sleep function), handler(for downloading the image while Async task is running) and Async task(for getting the JSON data from the URL). I have no idea how to resolve this issue, so any suggestions will be vey helpful.

This is the code for the thread which is executed on click of the button:

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 am using this libray and log-cat is: image

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

Nitish Jain
  • 704
  • 2
  • 10
  • 24
  • 1
    update with code and logcat plz – Robust Apr 16 '15 at 16:31
  • check this: http://stackoverflow.com/questions/7597742/what-is-the-purpose-of-looper-and-how-to-use-it – keshav kowshik Apr 16 '15 at 17:32
  • @Keshav1234: there are tons of good documentations about `Looper`'s. IMHO the question is: why does the method above expect the current thread to be a `Looper`? (here some nice docs about `Looper`'s: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/; http://mobilengineering.blogspot.de/2012/12/threading-loopers-and-messaging-queues.html; http://codetheory.in/android-handlers-runnables-loopers-messagequeue-handlerthread/) – Trinimon Apr 16 '15 at 17:37
  • Hello brother your post clearly states you did not get how to use looper in google hence that link was referred. Please check your question twice before posting. – keshav kowshik Apr 16 '15 at 17:42
  • @Keshav1234: I was afraid that your link might not be helpful as it refers rather to a newly created Looper thread. In this case OP needs most likely to use the already existing UI thread (from the links above: _"The most prominent usage of Pipeline Thread is found within almost any UI framework, including .... Android Activities."_). – Trinimon Apr 17 '15 at 06:02

1 Answers1

1

Try the following (unfortunately I cannot test 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 behind is, that the Timer thread is not a Looper thread (resulting in an exception saying "The current thread must have a looper"). The UI thread however, is a Looper thread (see for instance this site).

As flingContainer.getTopCardListener().selectLeft() is probably designed to run on the UI thread it fails, if it not invoked in side of a pipelined thread.

Trinimon
  • 13,839
  • 9
  • 44
  • 60