how to use runOnUiThread without getting "cannot make a static reference to the non static method" compiler error

android, multithreading

Solution

runOnUiThread is not a static method.

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

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

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

Problem

I have a main class; ``` ClientPlayer extends Activity { ``` and a service ``` LotteryServer extends Service implements Runnable { ``` when trying to use the RunOnUiThread in the run method of this service I am getting compiler error of, "cannot make a static reference to the non static method" how to fix this?, how I am using the code is shown here; ``` @Override public void run() { // I tried both ClientPlayer.runOnUiThread and LotteryServer.runOnUiThread // both don't work ClientPlayer.runOnUiThread(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "from inside thread", Toast.LENGTH_SHORT).show(); } }); } // end run method ```

Original source