Android “Only the original thread that created a view hierarchy can touch its views.”

后端 未结 28 3176
鱼传尺愫
鱼传尺愫 2020-11-21 04:44

I\'ve built a simple music player in Android. The view for each song contains a SeekBar, implemented like this:

public class Song extends Activity implement         


        
28条回答
  •  温柔的废话
    2020-11-21 05:13

    I've been in this situation, but I found a solution with the Handler Object.

    In my case, I want to update a ProgressDialog with the observer pattern. My view implements observer and overrides the update method.

    So, my main thread create the view and another thread call the update method that update the ProgressDialop and....:

    Only the original thread that created a view hierarchy can touch its views.

    It's possible to solve the problem with the Handler Object.

    Below, different parts of my code:

    public class ViewExecution extends Activity implements Observer{
    
        static final int PROGRESS_DIALOG = 0;
        ProgressDialog progressDialog;
        int currentNumber;
    
        public void onCreate(Bundle savedInstanceState) {
    
            currentNumber = 0;
            final Button launchPolicyButton =  ((Button) this.findViewById(R.id.launchButton));
            launchPolicyButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    showDialog(PROGRESS_DIALOG);
                }
            });
        }
    
        @Override
        protected Dialog onCreateDialog(int id) {
            switch(id) {
            case PROGRESS_DIALOG:
                progressDialog = new ProgressDialog(this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("Loading");
                progressDialog.setCancelable(true);
                return progressDialog;
            default:
                return null;
            }
        }
    
        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch(id) {
            case PROGRESS_DIALOG:
                progressDialog.setProgress(0);
            }
    
        }
    
        // Define the Handler that receives messages from the thread and update the progress
        final Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                int current = msg.arg1;
                progressDialog.setProgress(current);
                if (current >= 100){
                    removeDialog (PROGRESS_DIALOG);
                }
            }
        };
    
        // The method called by the observer (the second thread)
        @Override
        public void update(Observable obs, Object arg1) {
    
            Message msg = handler.obtainMessage();
            msg.arg1 = ++currentPluginNumber;
            handler.sendMessage(msg);
        }
    }
    

    This explanation can be found on this page, and you must read the "Example ProgressDialog with a second thread".

提交回复
热议问题