且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不获取“无法对非静态方法进行静态引用"的情况下使用runOnUiThread;编译器错误

更新时间:2023-11-13 07:52:39

runOnUiThread不是静态方法.

runOnUiThread is not a static method.

如果您想在UIThread上运行您的可运行对象,则可以使用此

If u want to run your runnable on UIThread You can use this

Handler handler = new Handler(Looper.getMainLooper());

Handler handler = new Handler(Looper.getMainLooper());

这将为UI线程创建一个处理程序.

This will create a handler for UI Thread.

ClientPlayer extends Activity {
.
.
public static Handler UIHandler;

static 
{
    UIHandler = new Handler(Looper.getMainLooper());
}
public static void runOnUI(Runnable runnable) {
    UIHandler.post(runnable);
}
.
.
.
}

现在您可以在任何地方使用它了.

Now u can use this anywhere.

@Override
public void run() {
   // I tried both ClientPlayer.runOnUiThread and LotteryServer.runOnUiThread
   // both don't work   
    ClientPlayer.runOnUI(new Runnable() {
        public void run() {
           Toast.makeText(getApplicationContext(), "from inside thread", Toast.LENGTH_SHORT).show();
        }
    });
} // end run method