程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 更新ui-求指點,Handler更新UI線程問題

更新ui-求指點,Handler更新UI線程問題

編輯:編程綜合問答
求指點,Handler更新UI線程問題

public Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        switch (msg.what) {
            case 0x123:
                one.setImageResource(imageIds[currentImageId++]);
                two.setImageResource(imageIds[currentImageId1++]);
                three.setImageResource(imageIds[currentImageId2++]);
                four.setImageResource(imageIds[currentImageId3++]);

                if (currentImageId >= imageIds.length) {
                    currentImageId = 0;
                }
                if (currentImageId1 >= imageIds.length) {
                    currentImageId1 = 0;
                }
                if (currentImageId2 >= imageIds.length) {
                    currentImageId2 = 0;
                }
                if (currentImageId3 >= imageIds.length) {
                    currentImageId3 = 0;
                }

                break;

            default:
                break;
        }
        linearLayout.invalidate();
    }

};
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yaobude);
    initView();

}

public void initView() {
    linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
    one = (ImageView) findViewById(R.id.one);
    two = (ImageView) findViewById(R.id.two);
    three = (ImageView) findViewById(R.id.three);
    four = (ImageView) findViewById(R.id.four);

    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);
    four.setOnClickListener(this);

    imageIds = new int[]{
            R.drawable.ee1,R.drawable.ee2,R.drawable.ee3,R.drawable.ee4,R.drawable.mmm2,R.drawable.mmm3
          };

    tt = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Message msg = new Message();
            msg.what = 0x123;
            System.out.println("----------"+msg.what);
            mHandler.handleMessage(msg);
        }
    };
    timer.schedule(tt, 0, period);
}

代碼如上,為啥還是會報不能更新ui線程的錯誤呢?求指點 thanks 報錯語句:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

最佳回答:


謝謝樓上 找到原因了 mHandler.handleMessage(msg); - -! mHandler.sendEmptyMessage(msg.what);

o19830015
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved