Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46368a28 that was originally added here

前端 未结 3 975
粉色の甜心
粉色の甜心 2020-12-06 06:03

This problem drives me crazy. I miss some basic but very important knowledge about how to handle long operations in a new thread created within an activity and how to modify

相关标签:
3条回答
  • 2020-12-06 06:37

    Sample code:

    new Thread(new Runnable() {                 
                    @Override
                    public void run() {
                        doNotUIthings();
                        updateUI.sendEmptyMessage(0);   //Update ui in UI thread
                    }
                    private Handler updateUI = new Handler(){
                        @Override
                        public void dispatchMessage(Message msg) {
                            super.dispatchMessage(msg);
                            updateUI();
                        }
                    };
                }).start();
    

    But I recommend to use AsyncTask instead of Java Threads.

    0 讨论(0)
  • 2020-12-06 06:46

    Your Handler needs to be created in your UI thread for it to be able to update the UI. I would then use the sendMessage method of the handler, rather than posting a runnable:

    private static final int HANDLER_MESSAGE_ERROR = 0;
    private static final int HANDLER_MESSAGE_COMPLETED = 1;
    ...
    private void connectAndGetRoute(){
        new Thread(){
            @Override
            public void run() {
                try {
                    if(!connectTo9292ov()) return;
    
                } catch(UnknownHostException e){
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } catch (ClientProtocolException e) {
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } catch (IOException e) {
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } finally {
                    sendMessage(HANDLER_MESSAGE_COMPLETED);
                }
            }
            private void sendMessage(int what){
                Message msg = Message.obtain();
                msg.what = what;
                mHandler.sendMessage(msg);
            }
        }.start();
    
    }
    ...
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
            case HANDLER_MESSAGE_ERROR:
                Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
                break;
            case HANDLER_MESSAGE_COMPLETED:
                mProgressDialog.dismiss();
                showOnScreen();
                break;
            default:
                Log.w("MyTag","Warning: message type \""+msg.what+"\" not supported");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 06:55

    Just a quick idea.

    i think, it's your toast-messages within thread. Try to comment them out.

    If you still want to show messages, save the status of your thread and handle it in your handler. Call for that your handler from finally block

    mProgressDialog = ProgressDialog.show(mContext, "Tripplanner", "please wait...", true, false);
    connectAndGetRoute();
    
    
    private void connectAndGetRoute(){
    
        new Thread(){
            @Override
            public void run() {
                try {
                if(!connectTo9292ov()) return;// conncetto9292ov() connects to a website, parses the reasult into an arraylist. The arraylist contains route.
    
                } catch(UnknownHostException e){
                    // an int member of your activity
                    threadStatus = 404;
                    // Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
                }catch (ClientProtocolException e) {
                    threadStatus = 404;
                   // Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();                   
            } catch (IOException e) {
                    threadStatus = 404;
                    // Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
                }
                finally {
                handler.post(runConnection);}
            }
    
        }.start();
    
        handler = new Handler();
        runConnection = new Runnable(){
            @Override
            public void run() {
                if (threadStatus == 404) { Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();}
                else {
                mProgressDialog.dismiss();
                showOnScreen();}
            }   
        };
    }
    
    0 讨论(0)
提交回复
热议问题