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

后端 未结 28 3210
鱼传尺愫
鱼传尺愫 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 see that you have accepted @providence's answer. Just in case, you can also use the handler too! First, do the int fields.

        private static final int SHOW_LOG = 1;
        private static final int HIDE_LOG = 0;
    

    Next, make a handler instance as a field.

        //TODO __________[ Handler ]__________
        @SuppressLint("HandlerLeak")
        protected Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                // Put code here...
    
                // Set a switch statement to toggle it on or off.
                switch(msg.what)
                {
                case SHOW_LOG:
                {
                    ads.setVisibility(View.VISIBLE);
                    break;
                }
                case HIDE_LOG:
                {
                    ads.setVisibility(View.GONE);
                    break;
                }
                }
            }
        };
    

    Make a method.

    //TODO __________[ Callbacks ]__________
    @Override
    public void showHandler(boolean show)
    {
        handler.sendEmptyMessage(show ? SHOW_LOG : HIDE_LOG);
    }
    

    Finally, put this at onCreate() method.

    showHandler(true);
    

提交回复
热议问题