2011-05-04 78 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

相关问题