2017-10-24 7 views
0

ジェスチャーに問題があります。私がアプリケーションをコンパイルするとエラーが発生しました:ジェスチャー認識Xamarn.Forms AndroidのSystem.NullReferenceException

System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。この行で

view.GestureRecognizers.Add(panGesture); 

私の全体のコード:

private ISwipeCallBack mISwipeCallback; 
    private double translatedX = 0, translatedY = 0; 

    public SwipeListener(View view , ISwipeCallBack iSwipeCallBack) 
    { 

     mISwipeCallback = iSwipeCallBack; 
     var panGesture = new PanGestureRecognizer(); 
     panGesture.PanUpdated += OnPanUpdated; 
     view.GestureRecognizers.Add(panGesture); 
    } 

    void OnPanUpdated(object sender, PanUpdatedEventArgs e) 
    { 

     View Content = (View)sender; 

     switch (e.StatusType) 
     { 

      case GestureStatus.Running: 

       try 
       { 
        translatedX = e.TotalX; 
        translatedY = e.TotalY; 
       } 
       catch (Exception err) 
       { 
        System.Diagnostics.Debug.WriteLine("" + err.Message); 
       } 
       break; 

      case GestureStatus.Completed: 

       System.Diagnostics.Debug.WriteLine("translatedX : " + translatedX); 
       System.Diagnostics.Debug.WriteLine("translatedY : " + translatedY); 

       if (translatedX < 0 && Math.Abs(translatedX) > Math.Abs(translatedY)) 
       { 
        mISwipeCallback.onLeftSwipe(Content); 
       } 
       else if (translatedX > 0 && translatedX > Math.Abs(translatedY)) 
       { 
        mISwipeCallback.onRightSwipe(Content); 
       } 
       else if (translatedY < 0 && Math.Abs(translatedY) > Math.Abs(translatedX)) 
       { 
        mISwipeCallback.onTopSwipe(Content); 
       } 
       else if (translatedY > 0 && translatedY > Math.Abs(translatedX)) 
       { 
        mISwipeCallback.onBottomSwipe(Content); 
       } 
       else 
       { 
        mISwipeCallback.onNothingSwiped(Content); 
       } 

       break; 

     } 
    } 

<StackLayout 
    > 

    <Label 
    x:Name="lbl_swipe" 
    Text="Welcome to Xamarin Forms! Drag it" 
    WidthRequest="300" 
    HeightRequest="200" 
    BackgroundColor="Yellow" 
    TextColor="Black" 
    FontSize="20" 
    VerticalOptions="Center" 
    HorizontalOptions="Center"> 
    </Label> 




    <Label 
    x:Name="lbl_result" 
    WidthRequest="300" 
    HeightRequest="100" 
    BackgroundColor="Purple" 
    TextColor="White" 
    FontSize="20" 
    VerticalOptions="End" 
    HorizontalOptions="Center"> 
    </Label> 

</StackLayout> 

メインページ

public partial class Testowy : ContentPage, ISwipeCallBack 
{ 
    public Testowy() 
    { 
     SwipeListener swipeListener = new SwipeListener(lbl_swipe, this); 
    } 

    public void onBottomSwipe(View view) 
    { 
     if (view == lbl_swipe) 
     { 
      lbl_result.Text = "OnBottomSwipe"; 
     } 
    } 

    public void onLeftSwipe(View view) 
    { 
     if (view == lbl_swipe) 
     { 
      lbl_result.Text = "onLeftSwipe"; 
     } 
    } 

    public void onNothingSwiped(View view) 
    { 
     if (view == lbl_swipe) 
     { 
      lbl_result.Text = "onNothingSwiped"; 
     } 
    } 

    public void onRightSwipe(View view) 
    { 
     if (view == lbl_swipe) 
     { 
      lbl_result.Text = "onRightSwipe"; 
     } 
    } 

    public void onTopSwipe(View view) 
    { 
     if (view == lbl_swipe) 
     { 
      lbl_result.Text = "onTopSwipe"; 
     } 
    } 
} 
+1

私の推測では、あなたのViewがnullになるように、あなたのラベルが作成される前にあなたのSwipeListenerを作成していると思います。 – Jason

答えて

2

あなたはInitializeComponent方法不足している - などlbl_resultやなどの参照を初期化する必要がありますlbl_swipe

public Testowy() 
{ 
    InitializeComponent(); //<---- add this line here. 
    SwipeListener swipeListener = new SwipeListener(lbl_swipe, this); 
} 
関連する問題