我有一个使用Thread发送我自己的事件的Observer Design pattern.有时被调用的类是AndroID活动,有时它们是常规类. Thread中的代码不是Activity.
我收到了这个错误:
Can’t create handler insIDe thread that has not called
Looper.prepare()
为了避免运行时错误,我不得不在Thread中添加Looper.prepare().我知道这篇文章:Can’t create handler inside thread that has not called Looper.prepare()它包含了这类问题的最佳信息,但我仍然不知道如何将mjosh的答案整合到我的主题中.
问题
从线程到活动的调用永远不会发生.孤立地,这个代码可以工作,但是当活动正在等待呼叫时.
该类将事件发送到其他类.这个用Thread运行.
public class EventBus{ // Non essential code here. ... // Thread code. private class MyThread implements Runnable { @OverrIDe public voID run() { while(true) { synchronized(List) { for(Observer o : List) // Execution transfers to Activity here. o.handleEvent(event); } } } }}
在活动中.
public class FirstActivity extends Activity implements Observer{ public voID handleEvent() { // Never gets here. }}
那么我该如何解决这个问题呢?我的整个应用程序将有许多活动和许多常规课程.
解决我的问题
我需要一种方法来保持我的线程活跃并调用活动.这可能是向Thread添加一些代码的混合,以便它可以成功地将我的调用发送到ActivitIEs.
解决方法:
虽然我没有为AndroID编程,但我认为在这里做你想做的事情并不困难.根据您链接问题中给出的答案,问题是您将拥有可能是也可能不是活动的观察者.由于Activity线程模型的性质,您无法简单地处理活动上的事件.对于活动,您需要在UI线程上完成 *** 作.这听起来相当紧张.使用POJO我在下面放了一个SSCCE.真正引起很大兴趣的唯一部分是在EventBus循环中发生的转换.
public class Test { public static voID main(String[] args) throws InterruptedException { //create the thread to demonstrate execution MyThread t = new MyThread(); //add a non activity observer t.addobserver(new NonActivityObserver()); //add an activity observer t.addobserver(new ActivityDecendent()); //fire off the thread new Thread(t).start(); //give us time to read the output Thread.sleep(Long.MAX_VALUE); } public static class MyThread implements Runnable { private ArrayList<Observer> List = new ArrayList<Observer>(); public voID addobserver(Observer arg0) { synchronized (List) { List.add(arg0); } } @OverrIDe public voID run() { //no loop just doing this once for example synchronized(List) { for(final Observer o : List) { //first try to cast to an Activity try { Activity act = (Activity)o; //if we get here, its an activity //so invoke on its UiThread act.runOnUiThread(new Runnable() { @OverrIDe public voID run() { Event event = new Event("From the activity's runOnUiThread method"); o.handleEvent(event); } }); } catch (Exception e) { //if we got here, the class is not an activity //so it should be safe to just handle the event //on this thread Event event = new Event("From observers handle event method"); o.handleEvent(event); } } } } } //your observer interface public static interface Observer { public voID handleEvent(Event e); } //Your Event class public static class Event { private String message; public Event(String message) { this.message = message; } public voID talk() { System.out.println(message); } } //An observer which isnt an activity public static class NonActivityObserver implements Observer { @OverrIDe public voID handleEvent(Event e) { e.talk(); } } //An activity which implements Observer public static class ActivityDecendent extends Activity implements Observer { @OverrIDe public voID handleEvent(Event e) { e.talk(); } } //An Activity public static class Activity { //pretend this is the AndroID activity executing this runnable //on the UI thread public voID runOnUiThread(Runnable r) { new Thread(r).start(); } }}
并输出:
From observers handle event methodFrom the activity's runOnUiThread method
这个解决方案特定于你的情况,这很糟糕……但我认为这应该有效.
总结以上是内存溢出为你收集整理的java – 如何从非活动线程调用活动?全部内容,希望文章能够帮你解决java – 如何从非活动线程调用活动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)