views:

706

answers:

1

Hello,

I want to create a simple android binary clock but my application crashes. I use 6 textview fields: 3 for the decimal and 3 for the binary representation of the current time (HH:mm:ss). Here's the code:

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) { };
 }
}`
+1  A: 
Adam
Thank you very much for the explanation and the code. Now things really make sense!
Hristo
no problem, thanks for posting your entire code block. much easier to see what's going on when you can load it yourself and muck around :D
Adam