2011-05-04 75 views
0

喂,Android的線程和處理程序,沒有發現錯誤

我'與新的線程,所以我讀到的一篇文章/教程... tutorial

我的目標是更新的TextView從線程的GUI ...

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。我認爲這個從未被調用過。

我不知道哪裏是我的錯..

感謝您

回答

0

我的印象中的System.out.println消息在Android平臺上「丟失」,從來沒有達成任何調試Eclipse中的窗口。相反,您應該使用Log類生成Logcat消息。

參見:

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

編輯:嗯,我剛纔注意到你還調用一個方法setTemperature()來更新您的TextView,但我似乎無法找到該方法的代碼清單。所以在這個階段,我不確定你的問題是你期望system.out.println在Logcat中產生什麼,或者你的setTemperature()方法有什麼問題。

您是否嘗試過在調試模式下使用斷點來查看處理程序是否被調用?據我一眼就可以看出,你的線程代碼似乎沒問題。

+0

嗯,我嘗試Log.d(...),和調試模式,但沒有。看起來handleMessage()永遠不會被調用。 setTemperature()fkt。並不重要(但我也改變了這一部分)。 Damm我不知道錯誤在哪裏.. – marcel 2011-05-04 18:17:21

+0

我發現我的錯誤,我用.run啓動線程而不是.start() – marcel 2011-05-05 13:24:18

相關問題