Android 错误:只有创建视图层次结构的原始线程才能在Xamarin中接触其视图
我收到一个错误:只有创建视图层次结构的原始线程才能接触其视图 一致Android 错误:只有创建视图层次结构的原始线程才能在Xamarin中接触其视图,android,xamarin,Android,Xamarin,我收到一个错误:只有创建视图层次结构的原始线程才能接触其视图 一致 BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader, listDataChild); try{ explistView.SetAdapter (expListAdapter); explistView.SetGroupIndicator (null); } catch(Exception e) { Toast.MakeText (th
BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader, listDataChild);
try{
explistView.SetAdapter (expListAdapter);
explistView.SetGroupIndicator (null);
}
catch(Exception e) {
Toast.MakeText (this,e+"",ToastLength.Long).Show();
}
这是我的一段代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.ServiceModel.Web;
using SalonServices;
using System.ServiceModel;
namespace PariSalon
{
[Activity (Label = "BookingRequest")]
public class BookingRequest : Activity
{
public List<GetServices> listDataHeader;
public List<GetServices> services;
Dictionary<GetServices, List<String>> listDataChild;
ExpandableListView explistView;
public static readonly EndpointAddress EndPoint = new EndpointAddress("http://xxx.xxx.Xx.xx/SalonService/SalonServices.svc");
//public static readonly EndpointAddress EndPoint = new EndpointAddress("http://xxxx");
private SalonServicesClient serviceClient;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.Booking_request);
InitializeSalonServiceClient();
Button submit = FindViewById<Button> (Resource.Id.button2);
//string userid = "1";
//serviceClient.GetServicesForUserAsync (userid);
string userid = "1";
serviceClient.GetServicesForUserAsync (userid);
//serviceClient.GetServicesForUserAsync (userid);
submit.Click += delegate {
Intent intent = new Intent(this, typeof(Booking_Request_sent));
StartActivity(intent);
};
// Create your application here
/*
explistView.SetOnGroupClickListener( new OnGroupClickListener());
//Catch click event attempt 2
explistView.GroupClick += (object pobjSender, ExpandableListView.GroupClickEventArgs pArgs) =>
{
int len = expListAdapter.GroupCount;
for(int i=0;i<len;i++)
{
if(i!=groupPosition)
{
explistView.CollapseGroup(i);
}
if(i==groupPosition){
if(explistView.IsGroupExpanded(groupPosition)){
explistView.CollapseGroup(groupPosition);
}
else {
explistView.ExpandGroup(groupPosition);
}
}
}
};*/
}
private void InitializeSalonServiceClient()
{
BasicHttpBinding binding = CreateBasicHttp();
serviceClient = new SalonServicesClient(binding, EndPoint);
//serviceClient.GetServicesForUserCompleted += GetServicesForUserCompleted;
//serviceClient.GetProductDetailsCompleted += GetProductDetailsCompleted;
serviceClient.GetServicesForUserCompleted += GetServicesForUserCompleted;
}
private void GetServicesForUserCompleted(object sender, GetServicesForUserCompletedEventArgs getServiceDataCompletedEventArgs)
{
string msg = null;
if (getServiceDataCompletedEventArgs.Error != null)
{
msg = getServiceDataCompletedEventArgs.Error.Message;
}
else if (getServiceDataCompletedEventArgs.Cancelled)
{
msg = "Request was cancelled.";
}
else
{
services = new List<GetServices>(getServiceDataCompletedEventArgs.Result);
prepareListData ();
explistView = FindViewById<ExpandableListView> (Resource.Id.expandableselectservicetype);
BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader, listDataChild);
try{
explistView.SetAdapter (expListAdapter);
explistView.SetGroupIndicator (null);
}
catch(Exception e) {
Toast.MakeText (this,e+"",ToastLength.Long).Show();
}
}
}
private static BasicHttpBinding CreateBasicHttp()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 50);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
private void prepareListData() {
listDataHeader = new List<GetServices>();
listDataChild = new Dictionary<GetServices, List<String>>();
listDataHeader.Clear ();
if (services != null) {
foreach (GetServices pd in services) {
listDataHeader.Add (pd);
}
// Adding child data
//listDataHeader.Add("Top 250/n");
//listDataHeader.Add("Now Showing");
//listDataHeader.Add("Coming Soon..");
List<String> childLabel = new List<String> ();
childLabel.Add ("Date :");
childLabel.Add ("Time :");
childLabel.Add ("User :");
childLabel.Add ("Second User:");
childLabel.Add ("Status :");
childLabel.Add ("Cancel ");
listDataChild.Add (listDataHeader.ElementAt (0), childLabel); // Header, Child data
listDataChild.Add (listDataHeader.ElementAt (1), childLabel);
listDataChild.Add (listDataHeader.ElementAt (2), childLabel);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用System.ServiceModel.Web;
使用美容服务;
使用System.ServiceModel;
巴黎沙龙酒店
{
[活动(Label=“BookingRequest”)]
公共类预订请求:活动
{
公共列表listDataHeader;
公开名单服务;
字典列表数据儿童;
ExpandableListView解释视图;
公共静态只读EndpointAddress EndPoint=新的EndpointAddress(“http://xxx.xxx.Xx.xx/SalonService/SalonServices.svc");
//公共静态只读EndpointAddress EndPoint=新的EndpointAddress(“http://xxxx");
私人沙龙服务客户服务客户;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(资源、布局、预订请求);
初始化LonServiceClient();
按钮提交=FindViewById(Resource.Id.button2);
//字符串userid=“1”;
//serviceClient.GetServicesForUserSync(用户ID);
字符串userid=“1”;
serviceClient.GetServicesForUserSync(用户ID);
//serviceClient.GetServicesForUserSync(用户ID);
提交。单击+=委派{
意向意向=新意向(此,类型(预订请求已发送));
星触觉(意向);
};
//在此处创建应用程序
/*
explistView.SetOnGroupClickListener(新的OnGroupClickListener());
//捕获单击事件尝试2
explistView.GroupClick+=(对象pobjSender,ExpandableListView.GroupClickEventArgs参数)=>
{
int len=expListAdapter.GroupCount;
对于(int i=0;i您正在从工作线程上的SalonServicesClient
调用GetServicesForsuercompleted
方法。改为在上调用它。已接受答案中的链接文章谈到了InvokeOnMainThread
,这是一个IOS的东西。不知道为什么它会被接受为Android的答案,因为问题是标签格德
对于Android,您使用的是Activity.RunOnUiThread
。相关文档如下:
因为您是从继承自活动的某个地方装配的,所以您只需包装相关代码即可:
BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader, listDataChild);
try{
explistView.SetAdapter (expListAdapter);
explistView.SetGroupIndicator (null);
}
catch(Exception e) {
Toast.MakeText (this,e+"",ToastLength.Long).Show();
}
变成:
RunOnUiThread(() =>
{
BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader, listDataChild);
try{
explistView.SetAdapter (expListAdapter);
explistView.SetGroupIndicator (null);
}
catch(Exception e) {
Toast.MakeText (this,e+"",ToastLength.Long).Show();
}
});
这就是它的全部内容。这里是xamarin针对这个问题的跨平台解决方案
Device.BeginInvokeOnMainThread(() =>
{
//Your code here
});
是的,我得到了答案,谢谢你的支持。+1感谢你的支持。提供的链接仅适用于iOS。它与问题所涉及的Android实现没有任何直接关系。