Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "runOnUiThread(runnable)" inside static method?

I'm writing code in two different classes. The first one runs IOIO Thread which reads pins status of an IOIO board; when this thread is running, it will update the several TextViews which are on the other class (Tab3Activity.java).

I called the method to update the UI just like the code below.

Tab3Activity.setText(index,"string here");

the setText() above need to be static otherwise it gives an err

Cannot make a static reference to the non-static method setText(int, String) from the type Tab3Activity

The problem is on the Tab3Activity.java.

public static void setText(final int idx,final String str) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            _textview[idx].setText(str);
        }
    });
}

the runOnUiThread above gives an err.

Cannot make a static reference to the non-static method runOnUiThread(Runnable) from the type Activity

This is the IOIO Thread code written in Globalioio.java, i'm trying to update the UI on the Tab3Activity.java. Look at the Loop() method.

class Looper extends BaseIOIOLooper {

    @Override
    public void setup() throws ConnectionLostException {
        //setup DigitalOutputs, AnalogInputs etc here.
        if(Tab2Activity.isOpened==true){
            led_ = ioio_.openDigitalOutput(0, true);

            pwm1S = ioio_.openPwmOutput(10, 100);
            pwm1S.setDutyCycle((float)Tab2Activity.pwm1Speed.getProgress()/100);
            pwm1Move = ioio_.openDigitalOutput(11, false);

            pwm2S = ioio_.openPwmOutput(12, 100);
            pwm2S.setDutyCycle((float)Tab2Activity.pwm2Speed.getProgress()/100);
            pwm2Move = ioio_.openDigitalOutput(13, false);

            pwmSrvo1 = ioio_.openPwmOutput(26, 100);
            pwmSrvo1.setDutyCycle((float)Tab2Activity.servo1.getProgress()/100);
            pwmSrvo2 = ioio_.openPwmOutput(27, 100);
            pwmSrvo2.setDutyCycle((float)Tab2Activity.servo2.getProgress()/100);
        }
        if(Tab3Activity.isOpened==true){
            sensor1 = ioio_.openAnalogInput(41);
            sensor2 = ioio_.openAnalogInput(42);
            for(int i = 0;i<30;i++){
                    dInput[i] = ioio_.openDigitalInput(DIGITAL_SENSOR_PIN[i]);
            }
            for(int i = 0; i<10;i++){
                aInput[i] = ioio_.openAnalogInput(ANALOG_SENSOR_PIN[i]);
            }
        }

        connStatus=true;
    }

    @Override
    public void loop() throws ConnectionLostException {
        try {
            if(Tab3Activity.slideDrawer2.isOpened()==true){
                final float range1 = (float)(2914/(sensor1.read() * 675.18+5))-1;
                Tab3Activity.setSeekBarSensor(0,(int) (range1));
                Tab3Activity.setTextSensor(0,Float.toString((range1)));
                final float range2 = (float)(2914/(sensor2.read() * 675.18+5))-1;
                Tab3Activity.setSeekBarSensor(1,(int) (range2));
                Tab3Activity.setTextSensor(1,Float.toString(range2));

            }
            if(Tab3Activity.slideDrawer1.isOpened()==true){
                if(Tab3Activity.pinsGroup==0){
                    int idx =0;
                    for(int i = 0;i<10;i++){
                       final boolean readingD = dInput[i].read();
                       if(readingD==true){
                           Tab3Activity.setSeekBar(idx,(int) (100));
                       }else{
                           Tab3Activity.setSeekBar(idx,(int) (0));
                       }
                       Tab3Activity.setText(idx,Boolean.toString(readingD));
                       idx++;
                    }
                }else if(Tab3Activity.pinsGroup==1){
                    int idx =0;
                    for(int i = 10;i<20;i++){
                          final boolean readingD = dInput[i].read();
                            if(readingD==true){
                                Tab3Activity.setSeekBar(idx,(int) (100));
                            }else{
                                Tab3Activity.setSeekBar(idx,(int) (0));
                           }
                            Tab3Activity.setText(idx,Boolean.toString(readingD));
                            idx++;
                        }
                }else if(Tab3Activity.pinsGroup==2){
                    int idx=0;
                    for(int i = 20;i<30;i++){
                          final boolean readingD = dInput[i].read();
                            if(readingD==true){
                                Tab3Activity.setSeekBar(idx,(int) (100));
                            }else{
                                Tab3Activity.setSeekBar(idx,(int) (0));
                           }
                            Tab3Activity.setText(idx,Boolean.toString(readingD));
                            idx++;
                    }
                }else if(Tab3Activity.pinsGroup==3){
                    int idx=0;
                    for(int i = 0;i<10;i++){
                        final float readingA = aInput[i].read();
                        Tab3Activity.setSeekBar(idx,(int) (readingA * 100));
                        Tab3Activity.setText(idx,Float.toString((readingA * 100)));
                        idx++;
                    }
                }
            }
            Thread.sleep(10);
        } catch (InterruptedException e) {
            ioio_.disconnect();
        } catch (ConnectionLostException e) {
            throw e;
        }
    }
}




@Override
public IOIOLooper createIOIOLooper(String arg0, Object arg1) {
    // TODO Auto-generated method stub
    return new Looper();
}

Is there any alternative to do this? please give the simple one, i'm quite new to android. Thanks in advance

like image 744
Adrianus Hendry Avatar asked Feb 16 '23 19:02

Adrianus Hendry


1 Answers

If this thread is started from the same activity

then you can pass the reference of the activity to the thread, and remove static from that method.

YourThread thread = new YourThread(yourActivity);
thread.start();

//YourThread
public class YourThread extends Thread
{
    Tab3Activity activity;
    public YourThread(Tab3Activity activity)
    {
       Tab3Activity.activity = activity;
    }

...
    activity.setText(index,"string here");
...
}

Note: Make sure your activity has android:configChanges="orientation|keyboardHidden|screenSize". Otherwise as you rotate your devices there will be a new instance of acitivity started.

And if your activity is not starting that thread

then you should not try to access the activity directly through a static method.

If you are sure about your implementation and if it does not lead to a memory leak or crash then try this

Create a static MainLooper Handler in your activity or anywhere.

public static Handler UIHandler = new Handler(Looper.getMainLooper());

now you can use this handler to run on ui thread.

public static void setText(final int idx,final String str) {
    UIHandler.post(new Runnable() {
        @Override
        public void run() {
            _textview[idx].setText(str);
        }
    });
}
like image 187
Vivek Khandelwal Avatar answered Mar 02 '23 22:03

Vivek Khandelwal