viv
viv

Reputation: 6177

Android: Only one Looper may be created per thread

I am having a problem with Android looper. I have a class that has extended AsynTask. Inside doInBackground() method i have Looper.prepare() and some code below.

It runs well and good for the first time but after that it gives an exception " Only one Looper may be created per thread" .

There seems some solution to use Looper.quit() but i am unable to implement it.

Any help will be appreciated.

Upvotes: 6

Views: 10026

Answers (4)

Aown Raza
Aown Raza

Reputation: 2023

Just add the following check.

 if (Looper.myLooper() == null)
     Looper.prepare();

Upvotes: 0

Michael Assraf
Michael Assraf

Reputation: 111

getActivity().runOnUiThread (new Thread(new Runnable() { 
    public void run() {
        Looper.myLooper();
        getActivity().invalidateOptionsMenu();
    }
}));

Upvotes: 0

qjbagu
qjbagu

Reputation: 31

class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

Upvotes: 3

sasykes
sasykes

Reputation: 66

Try...

Looper.getMainLooper().quit();

Upvotes: 0

Related Questions