android – CalledFromWrongThreadException

android – CalledFromWrongThreadException,第1张

概述public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ThraedDemo objDemo = new ThraedDemo(); Thread objThread = new
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上面的代码有什么问题.请给我一些建议.谢谢你提前

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

以上是内存溢出为你收集整理的android – CalledFromWrongThreadException全部内容,希望文章能够帮你解决android – CalledFromWrongThreadException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存