2014-01-16 32 views
2
原來的線程

我得到一個錯誤error: Only the original thread that created a view hierarchy can touch its views錯誤:只有創建視圖層次可以觸摸的意見Xamarin

符合

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); 
     } 
    } 


} 
} 

我有另一個適配器類,但我認爲錯誤只在這個類。我怎麼解決這個問題。任何人都可以幫我解決這個問題。

回答

0

您在工作線程上從SalonServicesClient調用GetServicesForUserCompleted方法。代之以在UI thread上調用它。

+0

是的我得到了答案感謝您的支持。 +1爲您的答案 –

+0

JFYI:http://stackoverflow.com/help/someone-answers –

+0

提供的鏈接僅適用於iOS。它與Android實施沒有任何直接關係,這與問題有關。 – nathanchere

3

被接受的答案中的鏈接文章談到了InvokeOnMainThread這是一個IOS的東西。不知道爲什麼它被接受爲Android的答案,因爲問題被標記。

對於Android,您可以使用Activity.RunOnUiThread。相關文件是在這裏:http://developer.xamarin.com/guides/android/advanced_topics/writing_responsive_applications/

因爲你是從什麼地方,從Activity繼承操縱這件事,你可以只是包裝相關的代碼,所以這樣的:

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(); 
    } 
} 

這是真的所有這一切都是爲了它。

相關問題