Android: Only one Looper may be created per thread

viv picture viv · Oct 8, 2010 · Viewed 9.7k times · Source

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.

Answer

qjbagu picture qjbagu · Dec 14, 2011
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();
      }
  }