Android的Activity.runOnUiThread不是静态的,那么我该如何使用它呢?

发布于 2024-12-06 10:32:38 字数 319 浏览 1 评论 0原文

例如,如果我有一个线程正在执行昂贵的操作,并且我想从该线程在 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 技术交流群。

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

发布评论

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

评论(6

和影子一齐双人舞 2024-12-13 10:32:38

劳纳克的想法是正确的。我只是补充一点,您还可以在方法 sendEmptyMessage 中指定一个整数作为处理程序的标识符。这将允许您创建一个可以处理所有 UI 更新的处理程序,例如

public static final int EXAMPLE = 0;
public static final int ANOTHER_EXAMPLE = 1;

private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch( msg.what ){
            case EXAMPLE: 
                //Perform action
                break;
            case ANOTHER_EXAMPLE;
                //Perform action
                break;
        }
    }
} 

//Call to submit handler requesting the first action be called
handler.sendEmptyMessage(EXAMPLE);

希望这会有所帮助!

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.

public static final int EXAMPLE = 0;
public static final int ANOTHER_EXAMPLE = 1;

private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch( msg.what ){
            case EXAMPLE: 
                //Perform action
                break;
            case ANOTHER_EXAMPLE;
                //Perform action
                break;
        }
    }
} 

//Call to submit handler requesting the first action be called
handler.sendEmptyMessage(EXAMPLE);

Hope this helps!

南风起 2024-12-13 10:32:38

您应该使用 Handler 类。处理程序类在 UI 线程上运行。当您完成线程中的工作后,请调用 handler.sendEmptyMessage(),您可以在其中对 ui 进行更改。

private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
         // make changes to 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.

private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
         // make changes to ui
    }
} 
人生百味 2024-12-13 10:32:38

您的问题并没有真正提供足够的细节,但从声音来看,您位于活动(Main)中的私有内部类(Runnable?)中。如果是这种情况,您可以写:

Main.this.runOnUiThread(mRunnable);

或者

runOnUiThread(mRunnable); //will see that there is no runOnUiThread in the current class and begin looking "upwards"

另外,您可能需要查看 AsyncTask,特别是在 UI 线程上运行的 onPostExecuteonPreExecuteonProgressUpdate 回调中。

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:

Main.this.runOnUiThread(mRunnable);

or

runOnUiThread(mRunnable); //will see that there is no runOnUiThread in the current class and begin looking "upwards"

Also, you may want to look at AsyncTask, specifically at the onPostExecute, onPreExecute and onProgressUpdate callbacks, which run on the UI thread.

落日海湾 2024-12-13 10:32:38

首先在 onCreate 之外创建一个可运行程序。像这样:

private Runnable myRunnable = new Runnable() {
        @Override
        public void run() {

                        //work to be done

        }
    };

然后使用以下命令调用可运行的:

runOnUiThread(myRunnable);

first create a runnable outside onCreate. Like this:

private Runnable myRunnable = new Runnable() {
        @Override
        public void run() {

                        //work to be done

        }
    };

and then call the runnable using:

runOnUiThread(myRunnable);
春风十里 2024-12-13 10:32:38

以上所有答案都不是很正确。

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(...)

最初的梦 2024-12-13 10:32:38

对于那些正在寻找简单即时解决方案的人,请按照简单的步骤

  • onCreate() 方法之前引用您的类

    MyClass obj;
    
  • onCreate() 方法中初始化它

    obj = MyClass.this;
    
  • 调用 runOnUiThread()

    obj.runOnUiThread(new Runnable() {
        公共无效运行(){
        //在这里执行你的UI任务
        }
    });
    

希望有帮助。

For those who are looking for an easy instant solution follow the simple steps

  • Make a reference of your class before your onCreate() method

    MyClass obj;
    
  • Initialize it in you onCreate() method

    obj = MyClass.this;
    
  • Call runOnUiThread()

    obj.runOnUiThread(new Runnable() {
        public void run() {
        //perform your UI tasks here
        }
    });
    

Hope it helps.

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