`Can't create handler…Looper.prepare()` in inherited Activity

前端 未结 1 1877
悲哀的现实
悲哀的现实 2021-01-16 04:41

I have a Game Activity (Activity A) that works well with all the code. Then I create a new Activity (Activity B) for my new game mode, that e

1条回答
  •  情话喂你
    2021-01-16 05:25

    Try this:

    Handler innerHandler;
    
    (new Thread(new Runnable() {
    
                @Override
                public void run() {
                    Looper.prepare();
    
                    innerHandler = new Handler() {
                        @Override
                        public void handleMessage(Message message) {
                            Toast.make(...);
                        }
    
                        @Override
                        public void dispatchMessage(Message message) {
                            handleMessage(message);
                        }
                    };
    
                    Message message = innerHandler.obtainMessage();
                    innerHandler.dispatchMessage(message);
                    Looper.loop();
                }
            })).start();
    

    There may be an easier way to handle the problem. Please refer to Android – Multithreading in a UI environment documentation.

    0 讨论(0)
提交回复
热议问题