Stopwatch, Getting several errors ive no idea how to fix them.

KeithnoloKeithnolo Member ✭✭
edited November 2018 in Xamarin.Android

Hi guys I am making a stopwatch application for my Mobile apps project, I found this piece of code online and decided to try it to see if it would work, I am getting several errors and I have no Idea how to fix them. Any help at all would be appreciated. I will post the pictures below.
Thanks for any help in advance.



Thanks again

Answers

  • stemadostemado Member ✭✭✭
    Can you upload your project? I'll take a look.
  • KeithnoloKeithnolo Member ✭✭

    Thanks for your reply :) I will past the code here, it wont let me upload the file to here.

  • KeithnoloKeithnolo Member ✭✭

    (activity_Main.xaml)

    <?xml version="1.0" encoding="utf-8"?>


        <Button
            android:text = "Start"
            android:layout_width = "0dp"
            android:layout_height = "wrap_content"
            android:layout_weight = "1"
            android:id = "@+id/btnStart"/>
    
        <Button
            android:text = "Pause"
            android:layout_width = "0dp"
            android:layout_height = "wrap_content"
            android:layout_weight = "1"
            android:id = "@+id/btnPause"/>
    
        <Button
            android:text = "Lap"
            android:layout_width = "0dp"
            android:layout_height = "wrap_content"
            android:layout_weight = "1"
            android:id = "@+id/btnLap"/>
    </LinearLayout>
    <TextView
        android:layout_gravity = "center_horizontal"
        android:id = "@+id/txtTimer"/>
    
    <LinearLayout
        android:orientation = "vertical"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content">
        <ScrollView
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content">
    
            <LinearLayout
                android:layout_height = "wrap_content"
                android:layout_width = "match_parent"
                android:orientation = "vertical"
                android:id ="@+id/container"/>
    
            </ScrollView>
    </LinearLayout>
    
  • KeithnoloKeithnolo Member ✭✭

    (MainActivity.cs)
    using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using System.Timers;
    using Android.Views;
    using Android.Content;

    namespace StopwatchTest
    {
    [Activity(Label = "StopwatchTest", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : Activity
    {
    Button btnStart, btnPause, btnLap;
    TextView txtTimer;
    LinearLayout container;
    Timer timer;

        int mins = 0, secs = 0;
        double milliSeconds = 1;
        int hours = 0;
    
    
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            //Set our view from the main layout resource
            //SetContentView(Resource_layout Main);
    
            btnStart = FindViewById<Button>(Resource.Id.btnStart);
            btnPause = FindViewById<Button>(Resource.Id.btnPause);
            btnLap = FindViewById<Button>(Resource.Id.btnLap);
            container = FindViewById<LinearLayout>(Resource.Id.container);
            txtTimer = FindViewById<TextView>(Resource.Id.container);
    
    
            btnStart.Click += delegate
            {
                timer = new Timer();
                timer.Interval = 1; // 1 milliseconds  
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            };
            btnPause.Click += delegate
            {
                timer.Stop();
                timer = null;
            };
    
    
            btnLap.Click += delegate
            {
                LayoutInflater inflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService);
                View addView = inflater.Inflate(Resource.Layout.row null);
                TextView txtContent = addView.FindViewById<TextView>(Resource.Id.textView1);
                txtContent.Text = txtTimer.Text;
                container.AddView(addView);
            };
    
            void Timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                milliSeconds++;
                if (milliSeconds >= 1000)
                {
                    secs++;
                    milliSeconds = 0;
                }
                if (secs == 59)
                {
                    mins++;
                    secs = 0;
                }
                if (mins == 59)
                {
                    hours++;
                    mins = 0;
                }
                RunOnUiThread(() =>
                {
                    txtTimer.Text = string.Format("(0)(0):(1:00):(2:000)", hours, mins, secs, milliSeconds);
                }
    
    
                );
            };
    
    
    
        }
    }
    

    }

  • KeithnoloKeithnolo Member ✭✭

    Thanks so much again for your help.

Sign In or Register to comment.