CalledFromWrongThreadException

时间:2010-08-05 09:52:02

标签: android

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ThraedDemo objDemo = new ThraedDemo();
    Thread objThread = new Thread() {

        @Override
        public void run() {

            objDemo.firstMethod();
        }
    };
    objThread.start();
}


class ThraedDemo {

        private void firstMethod() {
            Thread objThread = new Thread() {

                @Override
                public void run() {
                    try {
                    ((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);

                        Thread.sleep(10000);
                        Log.v("Thread","1111111111111111sleep");
                    } catch (InterruptedException ex) {
                        System.out.println("interuped exception" + ex.getMessage());
                    }
                    secondMethod();
                }

                private void secondMethod() {
        Thread objThread = new Thread() {

            @Override
            public void run() {
                try {
                ((ImageView)findViewById(R.id.ImageViewResult)).setImageResource(nums[n+1]);

                n++;

                    Thread.sleep(10000);
                   Log.v("Thread","22222222222 sleep");
                } catch (InterruptedException ex) {
                    System.out.println("interuped exception" + ex.getMessage());
                }
                firstMethod();
            }
        };
        objThread.start();
    }

            };
            objThread.start();
        }

    }

我使用上面的代码,但它没有运行。得到CalledFromWrongThreadException上面的代码有什么问题。请给我一些建议。谢谢提前

2 个答案:

答案 0 :(得分:10)

我认为您不能从UI线程以外的其他线程查看修改,因此要么在oncreate中创建handlers并将线程发布到它,要么使用AsyncTask或{{3将代码部分直接发送到UI线程的方法。

答案 1 :(得分:0)

我编辑了你的第二个函数代码,我看到你的代码永远是循环的,因为firstMethod调用secondMethod而secondMethod调用新的firstMethod来启动然后永远循环。我删除它并将代码更新ImageView移动到UI线程中,你可以试试这个:

class ThraedDemo {

    private void firstMethod() {
        Thread objThread = new Thread() {
            @Override
            public void run() {
                try {
                    runOnUiThread(new Runnable() {
                        public void run(){ 
                            ((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n]);  
                        }
                    });
                    Thread.sleep(10000);
                    Log.v("Thread","1111111111111111sleep");
                } catch (InterruptedException ex) {
                    System.out.println("interuped exception" + ex.getMessage());
                }
                secondMethod();
            }
        };
        objThread.start();
    }

    private void secondMethod() {
        Thread objThread2 = new Thread() {

            @Override
            public void run() {
                try {
                    runOnUiThread(new Runnable() {
                        public void run(){ 
                            ((ImageView)findViewById(R.id.ImageViewnumber)).setImageResource(nums[n+1]);  
                        }
                    });
                    n++;
                    Thread.sleep(10000);
                    Log.v("Thread","22222222222 sleep");
                } catch (InterruptedException ex) {
                    System.out.println("interuped exception" + ex.getMessage());
                }
            }
        };
        objThread2.start();
    }
}