3

好的,所以我知道这个一般问题已经在这里被问过很多次了,但我还没有找到一个对我有意义的答案。几乎我看到的每个答案都只是说一些模糊的东西,“嘿,只要把它扔进你的方法中,你就很好了”,但我没有看到完整的例子,而且我尝试过的也没有奏效。

这是我收到的错误:

[mono] android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

所以,简单地说,我有一个活动,它从 Web 服务中获取一些信息,然后将 Web 服务结果扔到几个 TextView 中。有人可以帮我弄清楚我需要在哪里以及如何使用RunOnUiThread()吗?这是代码:


using Android.App;
using Android.OS;
using System;
using System.Web;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Android.Widget;

namespace DispatchIntranet
{
    [Activity (Label = "@string/Summary")]          
    public class SummaryActivity : Activity
    {
        private static readonly Log LOG = new Log(typeof(SummaryActivity));
        private TextView summaryTotalRegularLabel;
        private TextView summaryTotalRollover;
        private TextView summaryScheduledLabel;
        private TextView summaryRemainingRegular;
        private string url;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // SET THE LAYOUT TO BE THE SUMMARY LAYOUT
            SetContentView(Resource.Layout.Summary);

            // INITIALIZE CLASS MEMBERS
            init();

            if (LOG.isInfoEnabled())
            {
                LOG.info("Making call to rest endpoint . . .");

                if (LOG.isDebugEnabled())
                {
                    LOG.debug("url: " + this.url);
                }
            }

            try
            {
                // BUILD REQUEST FROM URL
                HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(this.url));

                // SET METHOD TO 'GET'
                httpReq.Method = GetString(Resource.String.web_service_method_get);

                // ASK FOR JSON RESPONSE
                httpReq.Accept = GetString(Resource.String.web_service_method_accept);

                // INVOKE ASYNCHRONOUS WEB SERVICE
                httpReq.BeginGetResponse((ar) => {
                    HttpWebRequest request = (HttpWebRequest)ar.AsyncState;

                    using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse (ar))
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            // PUT RESPONSE INTO STRING
                            string content = reader.ReadToEnd();

                            // CONVERT STRING TO DYNAMIC JSON OBJECT
                            var json = JsonConvert.DeserializeObject<dynamic>(content);

                            if (LOG.isDebugEnabled())
                            {
                                LOG.debug("content: " + content);
                                LOG.debug("json: " + json);

                                LOG.debug("TOTAL_REGULAR_PTO_HOURS: " + json.d[0].TOTAL_REGULAR_PTO_HOURS);
                            }

                            // ** THIS IS WHAT WILL NOT WORK **
                            this.summaryTotalRegularLabel.Text = json.d[0].TOTAL_REGULAR_PTO_HOURS;
                            this.summaryTotalRollover.Text = json.d[0].TOTAL_ROLLOVER_PTO_HOURS;
                            this.summaryScheduledLabel.Text = json.d[0].TOTAL_USED_PTO_HOURS;
                            this.summaryRemainingRegular.Text = json.d[0].TOTAL_REMAINING_PTO_HOURS;
                        }
                    }
                }, httpReq);
            }
            catch (Exception e)
            {
                LOG.error("An exception occurred while attempting to call REST web service!", e);
            }
        }

        private void init()
        {
            // GET GUID FROM PREVIOUS INTENT AND DETERMINE CURRENT YEAR
            string guid = Intent.GetStringExtra("guid");
            int year = DateTime.Now.Year;

            // BUILD URL
            this.url = GetString(Resource.String.web_service_url)
                + GetString(Resource.String.ws_get_pto_summary)
                + "?" + "guid='" + HttpUtility.UrlEncode(guid) + "'"
                + "&" + "year=" + HttpUtility.UrlEncode(year.ToString());

            // GET THE SUMMARY LABELS
            this.summaryTotalRegularLabel = FindViewById<TextView>(Resource.Id.SummaryTotalRegular);
            this.summaryTotalRollover = FindViewById<TextView>(Resource.Id.summaryTotalRollover);
            this.summaryScheduledLabel = FindViewById<TextView>(Resource.Id.summaryScheduledLabel);
            this.summaryRemainingRegular = FindViewById<TextView>(Resource.Id.SummaryRemainingRegular);
        }
    }
}
4

1 回答 1

3

当您进行 Web 服务调用时,HttpWebRequest 会创建一个新线程来运行该操作。这样做是为了防止您的用户界面锁定或跳过帧。一旦您的 Web 服务调用完成,您需要返回 UI 线程以更新位于该线程上的 UI 组件。你可以通过几种不同的方式做到这一点。

首先,您可以将代码包装在匿名函数调用中,如下所示:

RunOnUiThread(()=>{
    this.summaryTotalRegularLabel.Text = json.d[0].TOTAL_REGULAR_PTO_HOURS;
    this.summaryTotalRollover.Text = json.d[0].TOTAL_ROLLOVER_PTO_HOURS;
    this.summaryScheduledLabel.Text = json.d[0].TOTAL_USED_PTO_HOURS;
    this.summaryRemainingRegular.Text = json.d[0].TOTAL_REMAINING_PTO_HOURS;
});

或者您可以通过 RunOnUiThread 调用函数(jsonPayload 是类上的一个字段):

jsonPayload = json;
RunOnUiThread(UpdateTextViews);

...


void UpdateTextViews()
{
    this.summaryTotalRegularLabel.Text = jsonPayload.d[0].TOTAL_REGULAR_PTO_HOURS;
    this.summaryTotalRollover.Text = jsonPayload.d[0].TOTAL_ROLLOVER_PTO_HOURS;
    this.summaryScheduledLabel.Text = jsonPayload.d[0].TOTAL_USED_PTO_HOURS;
    this.summaryRemainingRegular.Text = jsonPayload.d[0].TOTAL_REMAINING_PTO_HOURS;

}
于 2013-08-22T22:14:23.260 回答