2015-09-27 134 views
0

当模拟输出显示未处理的异常时,我的应用程序总是崩溃。 我是新来的Android和C#,所以有一点耐心将不胜感激!我的代码有什么问题?

namespace timer2 
{ 
    [Activity(Label = "timer2", MainLauncher = true, Icon = "@drawable/icon")] 

    public class MainActivity : Activity 
    { 
     static Button start, stop; 
     static TextView time; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      start = FindViewById<Button>(Resource.Id.start); 
      stop = FindViewById<Button>(Resource.Id.stop); 
      time = FindViewById<TextView>(Resource.Id.textView1); 

      SetContentView(Resource.Layout.Main); 


      time.Text = "00:03:00"; 

      Counterclass timer = new Counterclass(180000, 1000); 

      start.Click += delegate 
      { 
       timer.Start(); 
      }; 

      stop.Click += delegate 
      { 
       timer.Cancel(); 
      }; 
     } 

     public class Counterclass : CountDownTimer 
     { 
      public Counterclass(long millisInFuture, long countDownInterval)  :base(millisInFuture, countDownInterval) 
      { 
      } 

      public override void OnFinish() 
      { 
       time.Text = "Ready"; 
      } 

      public override void OnTick(long millisUntilFinished) 
      { 
       long millis = millisUntilFinished; 
       string hms = String.Format("%02d:%02d:%02d", TimeSpan.FromMilliseconds(millis).Hours, 
        TimeSpan.FromMilliseconds(millis).Minutes - TimeSpan.FromHours(TimeSpan.FromMilliseconds(millis).Hours).Minutes, 
        TimeSpan.FromMilliseconds(millis).Seconds - TimeSpan.FromMinutes(TimeSpan.FromMilliseconds(millis).Minutes).Seconds); 

       time.Text = hms; 
      } 
     } 
    } 
} 
+0

什么是未处理的异常?你想要达到什么目的? eplain你的问题陈述 – Viru

回答

3

您需要找到控件之前设置的内容视图:

 protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.Main); 

      start = FindViewById<Button>(Resource.Id.start); 
      stop = FindViewById<Button>(Resource.Id.stop); 
      time = FindViewById<TextView>(Resource.Id.textView1); 
+3

作为补充。不要将Button和EditText标记为静态。否则,参考文献将永远存在这可能是更大的应用程序中的问题。 –

+0

thx帮助我真的很感激它! –