Only one Looper may be created per thread

来源:互联网 发布:在线视频变速播放软件 编辑:程序博客网 时间:2024/05/20 06:31
java.lang.RuntimeException: Only one Looper may be created per thread
at android.os.Looper.prepare(Looper.java:77)
at android.os.Looper.prepare(Looper.java:72)
at com.tele.test.MyHandlerTask$2.run(MyHandlerTask.java:36)
at com.tele.test.MyHandlerTask.testLooper(MyHandlerTask.java:40)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)

at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)


原因分析:

Looper has nothing to do with for loops. The Looper is the part of the Android system that controls the UI thread. There are several things that wont work without preparing the looper, but in general it is best to avoid Looper.prepare(); unless absolutly nessisary. It is far better to use the async doInBackground to perform data processing or other longer operations, and then update the UI with onPostExecute and onProgressUpdate.

In short, unless you are using the UI in some way, you don't need to call the Looper. If you find yourself having to call the Looper, you probably need to restructure how your background threads are working.

意思是说:一个线程只能有一个Looper.prepare的方法,而Looper.myLooper必须是在Looper.prepare之后才可以使用,因为Looper.prepare会new一个Looper。




0 0
原创粉丝点击