2017-04-24 11 views
0

何かがクリックされたときにレイアウトを操作しようとしているため、ハンドラクラスを使用する必要があります。問題は、私はこのメッセージを取得せずにそれを行うことができないです。非アクティビティクラスからビューを追加する際に例外が発生する

Screen capture of the error message

はここに私のコードです。それらはMainActivityクラスに配置されている場合

public class ClickListener : VectorElementEventListener 
{ 
    MapView mapView; 
    MainActivity activity; 

    public ClickListener(MainActivity activity) 
    { 
     this.mapView = activity.mapView; 
     this.activity = activity; 
    } 

    public override bool OnVectorElementClicked(VectorElementClickInfo clickInfo) 
    { 
     var zoomIn = activity.FindViewById<Button>(Resource.Id.zoomIn); 
     zoomIn.Text = "+"; // The exception originates from this line 

     return true; 
    } 
} 

を(私はC#/ Xamarin.androidを使用しています)これらの2行は正常に動作します。何故ですか?

私はこれに長続きしてきました。助けてください!

+0

助けにあなたのエラーログを提供してくださいが、私はあなたがFindViewById <>アクティビティクラスではないと呼ばれると思いますが、ビューの参照を渡すことができます –

答えて

1

私はそれを得ました。解決方法は次のとおりです。 UIを変更したい場合は、アクションを使用してメインのアクティビティUIスレッドで実行する必要があります。このように:

public class ClickListener : VectorElementEventListener 
{ 
    MapView mapView; 
    MainActivity activity; 

    public ClickListener(MainActivity activity) 
    { 
     this.mapView = activity.mapView; 
     this.activity = activity; 
    } 

    public override bool OnVectorElementClicked(VectorElementClickInfo clickInfo) 
    { 
     Action manipUI =() => { 
      var zoomIn = activity.FindViewById<Button>(Resource.Id.zoomIn); 
      zoomIn.Text = "+"; 
     } 
     activity.RunOnUIThread(manipUI); 
     return true; 
    } 
} 
関連する問題