如何处理观察者中的数据输出?

发布于 2024-08-28 06:33:31 字数 1088 浏览 4 评论 0原文

我有一个 Observable 和一个 Observer。 observable 会在后台线程中下载一些内容,并调用notifyObservers 让观察者读取状态。

在 public void update 中的某个时刻,观察者尝试更新 GUI

((TextView)findViewById('R.id.foo')).setText("bar");

,但看起来可观察线程调用此方法,因为 Observable (!!!) 抛出此错误:

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
 at android.view.ViewRoot.checkThread(ViewRoot.java:2462)
 at android.view.ViewRoot.requestLayout(ViewRoot.java:512)
 ...
 at com.mynamespace.acitivty.TrackActivity.startPlay(TrackActivity.java:72)
 at com.mynamespace.acitivty.TrackActivity.update(TrackActivity.java:107)
 at java.util.Observable.notifyObservers(Observable.java:147)
 at java.util.Observable.notifyObservers(Observable.java:128)
 at com.mynamespace.module.communication.Download.stateChanged(Download.java:213)
 at com.mynamespace.module.communication.Download.run(Download.java:186)
 at java.lang.Thread.run(Thread.java:1058)

有什么方法可以阻止这会发生吗?我确信我在这里遗漏了一些明显的东西。

I have an Observable and an Observer. The observable does download some stuff in a background thread and calls notifyObservers to let the observers read the status.

At some point in public void update the observer tries to updates the GUI

((TextView)findViewById('R.id.foo')).setText("bar");

but it seems like the observable thread calls this method, because the Observable (!!!) throws this:

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
 at android.view.ViewRoot.checkThread(ViewRoot.java:2462)
 at android.view.ViewRoot.requestLayout(ViewRoot.java:512)
 ...
 at com.mynamespace.acitivty.TrackActivity.startPlay(TrackActivity.java:72)
 at com.mynamespace.acitivty.TrackActivity.update(TrackActivity.java:107)
 at java.util.Observable.notifyObservers(Observable.java:147)
 at java.util.Observable.notifyObservers(Observable.java:128)
 at com.mynamespace.module.communication.Download.stateChanged(Download.java:213)
 at com.mynamespace.module.communication.Download.run(Download.java:186)
 at java.lang.Thread.run(Thread.java:1058)

Is there some way I can prevent this from happening? I'm sure I'm missing something obvious here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-09-04 06:33:31

我认为您的问题与此类似:Android:android.view。 ViewRoot$CalledFromWrongThreadException - 如何解决问题?

由于 bhatt4982 在那里回答,您可以使用 处理程序。从它的文档来看:

“处理程序有两个主要用途:(1)安排消息和可运行对象在将来的某个时刻执行;(2)将要在与您自己的线程不同的线程上执行的操作排队.”。

AshtonBRSC 的答案也很好,您可以将 UI 更新包含在 Runnable 中并使用 runOnUiThread Activity 的方法。

I think your issue is similar to this one: Android:android.view.ViewRoot$CalledFromWrongThreadException - How to solve the problem?

As bhatt4982 answers there, you can use Handler. From its doc:

"There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.".

The answer from AshtonBRSC is also good, you can enclose your UI update in a Runnable and use the runOnUiThread Activity's method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文