Android的Activity.runOnUiThread不是静态的,那么我该如何使用它呢?
例如,如果我有一个线程正在执行昂贵的操作,并且我想从该线程在 Main(活动)类中触发 runOnUiThread。显然我不应该创建我的活动类(主要)的实例。因此,如果我
Main.runOnUiThread(mRunnable);
从我的线程中尝试,它会给我一个错误,指出它不是静态方法,因此无法以我的方式访问它。现在我的理解是,活动类几乎几乎是以静态方式访问的。
我该怎么做?
(顺便说一句:我这样做是因为我收到 CalledFromWrongThreadException,只有创建视图层次结构的原始线程才能触摸它的视图)
For example, if I have a thread doing expensive stuff, and from that thread I want to fire runOnUiThread in the Main (activity) class. Obviously I shouldn't make an instance of my activity class (Main). So if I try
Main.runOnUiThread(mRunnable);
from my thread it gives me an error saying it's not a static method, and therefor it can't be accessed in my way. Now my understanding would be that the activity class is nearly almost accessed in a static way.
How would I do this?
(Btw: I'm doing this because I was getting CalledFromWrongThreadException, Only the original thread that created a view hierarchy can touch it's views)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
劳纳克的想法是正确的。我只是补充一点,您还可以在方法 sendEmptyMessage 中指定一个整数作为处理程序的标识符。这将允许您创建一个可以处理所有 UI 更新的处理程序,例如
希望这会有所帮助!
Raunak has the right idea. I'll just add that you can also specify an integer in the method sendEmptyMessage as an identifier to the handler. This will allow you to create one handler that can handle all of your UI updates, e.g.
Hope this helps!
您应该使用 Handler 类。处理程序类在 UI 线程上运行。当您完成线程中的工作后,请调用 handler.sendEmptyMessage(),您可以在其中对 ui 进行更改。
You should use the Handler class. The handler class runs on the UI thread. When you finish work in your thread, call
handler.sendEmptyMessage()
, from where you can make the changes to your ui.您的问题并没有真正提供足够的细节,但从声音来看,您位于活动(Main)中的私有内部类(Runnable?)中。如果是这种情况,您可以写:
或者
另外,您可能需要查看 AsyncTask,特别是在 UI 线程上运行的
onPostExecute
、onPreExecute
和onProgressUpdate
回调中。Your question doesn't really provide enough details, but from the sound of things, you're in a private inner class (Runnable?) in your activity (Main). If that is the case, you can either write:
or
Also, you may want to look at AsyncTask, specifically at the
onPostExecute
,onPreExecute
andonProgressUpdate
callbacks, which run on the UI thread.首先在 onCreate 之外创建一个可运行程序。像这样:
然后使用以下命令调用可运行的:
first create a runnable outside onCreate. Like this:
and then call the runnable using:
以上所有答案都不是很正确。
1)如果您希望一段代码在任何线程代码库的 UI 线程上运行。你可以这样做:
Looper.getMainLooper().post(new Runnable(...))
因为Looper.getMainLooper()是一个静态变量,并且在ActivityThread中初始化。
2) 如果您的可运行代码片段位于 Activity 内
那么你可以使用:
MainActivity.this.runOnUiThread(...)
all of the above answers are not very correct.
1)if you want a piece of code to run on UI thread from any thread code base. you can do:
Looper.getMainLooper().post(new Runnable(...))
because Looper.getMainLooper() is a static variable and initialized in ActivityThread.
2) if your runnable code snippet is inside an activity
then you can use:
MainActivity.this.runOnUiThread(...)
对于那些正在寻找简单即时解决方案的人,请按照简单的步骤
在
onCreate()
方法之前引用您的类在
onCreate()
方法中初始化它调用
runOnUiThread()
希望有帮助。
For those who are looking for an easy instant solution follow the simple steps
Make a reference of your class before your
onCreate()
methodInitialize it in you
onCreate()
methodCall
runOnUiThread()
Hope it helps.