Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Exception: Only the original thread that created a view hierarchy can touch its views

$
0
0

The code below works on Windows Phone but when running on Android I am getting the following exception : android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

        public ButtonLoggerPage()
                {
                    // Create the Button and attach Clicked handler.
                    Button button = new Button
                    {
                        Text = "Log the Click Time"
                    };

                    button.Clicked += OnButtonClicked;
                    opusClient.LoginCheckCompleted += OpenReadLogincheck;

                    this.Padding = new Thickness(5, Device.OnPlatform(20, 0, 0), 5, 0);

                    tbSyncItemLbl = new Label { };

                    // Assemble the page.
                    this.Content = new StackLayout
                    {
                        Children = { button,new ScrollView { VerticalOptions = LayoutOptions.FillAndExpand,

                        BackgroundColor = Color.Gray,
                        Content = loggerLayout}
                        }
                    };
                }

        void OnButtonClicked(object sender, EventArgs args)
            {

                        loginCheck();

            }

     public void loginCheck()
            {
                try
                {


                    client.LoginCheckAsync(username, password);
                }
                catch (Exception e)
                {

                    receiveErrors = 22;
                }
            }

    void OpenReadLogincheck(Object sender, LoginCheckCompletedEventArgs e)
    {


                if (e.Result != null)
                {
                    LoginStatus = e.Result.Value;
                    if (e.Result == 1)
                    {
                        addMessage("Login Succeeded", Direction.Download);

                    }

}
}

        public enum Direction { Upload, Download, DownloadFailed, UploadFailed };

             public void setSyncItem(string itemName, Direction dir)
                    {
                        tbSyncItemLbl.Text += itemName +" " + Environment.NewLine;


                        if (dir == Direction.Download)
                        {
                            this.tbSyncItemLbl.TextColor = Color.FromHex("B5DF26");
                        }
                        else if (dir == Direction.Upload)
                        {
                            this.tbSyncItemLbl.TextColor = Color.FromHex("527EA5");
                        }
                        else if (dir == Direction.DownloadFailed)
                        {
                           this.tbSyncItemLbl.TextColor = Color.FromHex("FF0000");
                        }
                        else if (dir == Direction.UploadFailed)
                        {
                           this.tbSyncItemLbl.TextColor = Color.FromHex("FF0000");
                        }

                        this.tbSyncItemLbl.YAlign = TextAlignment.Center;

                    }

         private void addMessage(string item, Direction dir)
                {
                          setSyncItem(item, dir);
                        loggerLayout.Children.Add(tbSyncItemLbl);                   
                }

        Label tbSyncItemLbl ;

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>