Android: Why can't I create a handler in new thread

后端 未结 4 998
北荒
北荒 2020-12-14 01:11

I had a problem creating a handler in new thread. This is my code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 02:02

    You could also use a HandlerThread like this:

    HandlerThread thread = new HandlerThread("MyHandlerThread");
    thread.start();
    Handler handler = new Handler(thread.getLooper());
    

    HandlerThreads have a Looper associated with them, so this wouldn't throw an exception.

提交回复
热议问题