CalledFromWrongThreadException [英] CalledFromWrongThreadException

查看:143
本文介绍了CalledFromWrongThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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();
        }

    }

我用上面的code,但它不是running.it了 CalledFromWrongThreadException 什么是INB上述$的问题,C $ C.手电筒给我一些建议提前.Thanks

I use the above code but it is not running.it got CalledFromWrongThreadException what is the problem inb the above code.Please give me some suggestions.Thanks in advance

推荐答案

我觉得不能从另一个线程比UI线程做观点的修改,所以无论是创建的 的处理程序在OnCreate并发布您的线程,或者使用的AsyncTask 或<一href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29">runOnUIThread方法发送的部分code直接到UI线程。

I think you can't do view modifications from another thread than the UI thread, so either create handlers in the oncreate and post your thread to it, or use AsyncTask, or runOnUIThread method to send portions of code directly to the UI thread.

这篇关于CalledFromWrongThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆