gpt4 book ai didi

java - 如何在静态方法中使用 "runOnUiThread(runnable)"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:17 30 4
gpt4 key购买 nike

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

我调用了更新 UI 的方法,就像下面的代码一样。

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

上面的 setText() 需要是静态的,否则会报错

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

问题出在 Tab3Activity.java 上。

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

上面的 runOnUiThread 报错。

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

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

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

有没有其他方法可以做到这一点?请给出简单的一个,我对android很陌生。提前致谢

最佳答案

If this thread is started from the same activity

然后您可以将 Activity 的引用传递给线程,并从该方法中删除静态。

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

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

And if your activity is not starting that thread

那么您不应该尝试通过静态方法直接访问 Activity 。

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

在您的 Activity 中或任何地方创建静态 MainLooper 处理程序。

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

关于java - 如何在静态方法中使用 "runOnUiThread(runnable)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061333/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com