Android线程和处理程序,没有发现错误

时间:2011-05-04 14:54:47

标签: android

喂,

我是线程新手,所以我读了一篇关于它的文章/教程...... tutorial

我的目标是从线程中更新gui中的textview ...

public class recorderThread extends Thread {
public boolean recording = true;  //variable to start or stop recording
public int frequency; //the public variable that contains the frequency value "heard", it is updated continually while the thread is     running.
int numSamples;
AudioRecord recorder;
int numCrossing,p;
short audioData[];
int bufferSize;
private Handler parentHandler;

public recorderThread (Handler parentHandle ) {
 this.parentHandler = parentHandle;
}    
@Override
public void run() {
doingSuff();
Message msgn = new Message() ;
msgn.arg1 = frequency;  
/* Sending the message */
parentHandler.sendMessage(msgn);
} //while recording



  }//run   
}//recorderThread

活动

public class sonicDistance extends Activity {


recorderThread rthread ;
static TextView freq;
static TextView duration;
static TextView samplerate;
private static TextView temperature;
static TextView measuredDistance;
private static TextView measuredTimeDiff;
public static TextView measuredFrequenz;
private CustomDialog myfreqDialog;
static double startTime = 0;
static double endTime = 0;
private static float c_propagation = 0;
private static float choosen_temperature = 20;
static double messureFreq = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    freq =((TextView) findViewById(R.id._freqOfTone));
    duration =((TextView) findViewById(R.id.duration));
    samplerate = ((TextView) findViewById(R.id.Samplerate));
    temperature  = ((TextView) findViewById(R.id.temperature));
    measuredFrequenz = ((TextView) findViewById(R.id._messuredFrequenz));
    measuredDistance = ((TextView) findViewById(R.id.distance));
    measuredTimeDiff = ((TextView) findViewById(R.id._timediff));

     rthread = new recorderThread(myHandler);
     rthead.run();

    }
public Handler myHandler = new Handler(){
    public void handleMessage(Message msg) {
            System.out.println(msg.arg1);
            setTemperature(Float.valueOf((String)msg.obj));
            System.out.println("test" + Float.valueOf((String)msg.obj));
    }

    };


@Override
protected void onResume(){
    super.onResume();
}

}

处理程序的活动中没有system.out。我认为这个从未被称为。

我不知道我的错误在哪里..

谢谢你

1 个答案:

答案 0 :(得分:0)

我的印象是system.out.println消息在Android平台上“丢失”,并且从未到达Eclipse中的任何调试窗口。相反,您应该使用Log类来生成Logcat消息。

请参阅:

Why doesn't "System.out.println" work in Android?

编辑:啊,我刚刚注意到你也在调用方法setTemperature()来更新你的TextView,但我似乎无法在你的代码清单中找到该方法。所以在这个阶段,我不确定你的问题是你是否期望system.out.println在Logcat中产生一些东西,或者你的setTemperature()方法出了什么问题。

您是否尝试在调试模式下使用断点来查看是否正在调用处理程序?据我所知,你的线程代码似乎没问题。