如何使用“runOnUiThread(runnable)”内部静态方法? [英] How to use "runOnUiThread(runnable)" inside static method?

查看:173
本文介绍了如何使用“runOnUiThread(runnable)”内部静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在两个不同的类编写代码。第一个运行IOIO线程,读取IOIO板的引脚状态;当这个线程正在运行时,它将更新在另一个类(Tab3Activity.java)上的几个TextView。



我调用了更新UI的方法,就像

  Tab3Activity.setText(index,string here); 

上面的 setText()需要static否则给出错误


不能从类型Tab3Activity <静态方法>静态引用非静态方法setText(int,String) / p>

问题出在Tab3Activity.java上。

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

runOnUiThread 给出一个错误。


无法从类型Activity

$中的非静态方法runOnUiThread(Runnable) b $ b

这是在Globalioio.java中编写的IOIO线程代码,我试图更新Tab3Activity.java上的UI。查看 Loop()方法。

  class Looper extends BaseIOIOLooper { 

@Override
public void setup()throws ConnectionLostException {
//设置DigitalOutputs,AnalogInputs等。
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); (int i = 0; i <30; i ++){
dInput [i] = ioio_.openDigitalInput(DIGITAL_SENSOR_PIN [i]);
(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; (int i = 0; i< 10; i ++){
final boolean readD = 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; (int i = 10; i <20; i ++){
final boolean readD = 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; (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; (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自动生成的方法stub
return new Looper();
}

有没有办法做到这一点?
请给我一个简单的一个,我是Android的新手。感谢提前

解决方案


如果此线程从相同的活动开始


那么你可以将活动的引用传递给线程,并从该方法中删除静态。

  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);
...
}

注意确保你的活动有android:configChanges =orientation | keyboardHidden | screenSize。否则,当您旋转设备时,将会启动一个新的活动实例。


如果您的活动没有启动该线程


那么你不应该通过静态方法直接访问活动。



如果你确定你的实现,如果它不会导致内存泄漏或崩溃,那么尝试这个



在你的活动或任何地方创建一个静态MainLooper处理程序。 p>

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

现在您可以使用此处理程序在ui线程上运行。

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


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

解决方案

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);
        }
    });
}

这篇关于如何使用“runOnUiThread(runnable)”内部静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆