2010-10-08 37 views
6

我遇到Android Looper问题。我有一个扩展AsynTask的类。在doInBackground()方法里面我有Looper.prepare()和下面的一些代码。Android:每个线程只能创建一个Looper

它第一次运行良好,但之后出现异常 “每个线程只能创建一个Looper”。

似乎有一些解决方案使用Looper.quit(),但我无法实现它。

任何帮助将不胜感激。

+1

在我看来,Looper.quit()是你在这里需要的。在android API文档中,明确指出“在调用此方法之后确保调用loop(),并通过调用quit()”来结束它。 – onlygo 2010-10-08 07:39:39

+0

你是什么意思“我无法实现它”? – methode 2010-10-08 07:49:05

+0

我的意思是我无法在Looper中找到quit()。它显示了getMainLooper,myLooper(),loop(),myQueue(),prepare(),prepareMainLooper()但不退出()。 – viv 2010-10-08 07:58:10

回答

0

的Try ...

Looper.getMainLooper().quit(); 
3
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(); 
     } 
    } 
0
getActivity().runOnUiThread (new Thread(new Runnable() { 
    public void run() { 
     Looper.myLooper(); 
     getActivity().invalidateOptionsMenu(); 
    } 
})); 
0

只需添加以下检查。

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

http://stackoverflow.com/questions/23038682/java-lang-runtimeexception-only-one-looper-may-be-created-per-thread/24115631#24115631 – 2015-09-01 12:01:22

相关问题