2015-09-27 177 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幫助我真的很感激它! –