我想創建一個簡單的android二進制時鐘,但我的應用程序崩潰。 我使用6個textview字段:3爲小數,3爲當前時間的二進制表示(HH:mm:ss)。 下面的代碼:簡單的Android二進制文本時鐘
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Binary extends Activity implements Runnable
{
Thread runner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (runner == null)
{ //start the song
runner = new Thread(this);
runner.start();
}
}
@Override
public void run()
{
TextView hours_dec = (TextView) findViewById(R.id.hours_dec);
TextView mins_dec = (TextView) findViewById(R.id.mins_dec);
TextView secs_dec = (TextView) findViewById(R.id.secs_dec);
TextView hours_bin = (TextView) findViewById(R.id.hours_bin);
TextView mins_bin = (TextView) findViewById(R.id.mins_bin);
TextView secs_bin = (TextView) findViewById(R.id.secs_bin);
SimpleDateFormat hours_sdf = new SimpleDateFormat("HH");
SimpleDateFormat mins_sdf = new SimpleDateFormat("mm");
SimpleDateFormat secs_sdf = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
while (runner != null)
{
WaitAMoment();
cal.getTime();
hours_dec.setText(hours_sdf.format(cal.getTime()));
mins_dec.setText(mins_sdf.format(cal.getTime()));
secs_dec.setText(secs_sdf.format(cal.getTime()));
hours_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) hours_dec.getText()))));
mins_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) mins_dec.getText()))));
secs_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) secs_dec.getText()))));
}
}
protected void WaitAMoment()
{
try
{
Thread.sleep(100);
} catch (InterruptedException e) { };
}
}`
當一個應用程序崩潰,您可以隨時看從模擬器或真實設備輸出的日誌。它在哪裏崩潰? – 2010-05-13 22:45:17
具體而言,在Eclipse中使用'adb logcat',DDMS或DDMS透視圖來查找Java堆棧跟蹤。查找「引發者:」行以查找異常。 – CommonsWare 2010-05-13 23:53:33
感謝您的回覆!是的,我通常使用LogCat來追蹤錯誤,但是我無法使用LogCat輸出來瞭解目前的錯誤。 – User3419 2010-05-14 08:07:36