简体   繁体   English

我的代码有什么问题?

[英]what wrong with my code?

My app always crashes when emulating the output shows unhandled exception. 当模拟输出显示未处理的异常时,我的应用程序总是崩溃。 I'm new to android and c#, so a little patience will be appreciated!! 我是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;
            }
        }
    }
}

You need to set the content view before finding the controls: 您需要先设置内容视图,然后才能找到控件:

        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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM