几秒后,我的Android应用程序启动,出现异常消息

时间:2017-02-14 15:06:12

标签: c# visual-studio xamarin.android thread-safety

(我使用visual studio 2015 Xamarin)

这是几秒钟后我看到我的Android应用程序启动的消息:

" Android.Util.AndroidRuntimeException:只有创建视图层次结构的原始线程才能触及其视图。 "

- 我的应用程序应该在TextView中连续写入当前日期和时间。 这是我写的代码..错误在哪里?我该如何解决?

namespace App4
{
    [Activity(Label = "app4", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private delegate void delegato();
        private delegato del1;

        public MainActivity()
        {
            del1 = getdatehour;
        }
        private void Do()
        {
            del1();
        }
        private void getdatehour()
        {
            TextView datehour = (TextView)FindViewById(Resource.Id.textView1);
            DateTime date;
            while (true)
            {
                date = DateTime.Now;
                String s = "DATA E ORA: " + date;
                datehour.Text = s;
            }
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            Thread thdo = new Thread(Do);
            thdo.Start(); 
        }
    }
}

0 个答案:

没有答案
相关问题