2013-05-18 3 views
0

사용자가 왼쪽에서 오른쪽으로 튀어 오를 때 내 앱이 처음으로 충돌합니다. 그러나 그들이 왼쪽 던지기 권리를한다면 그것은 잘 작동합니다. 내가 만든 gridView에서만이 작업을 수행합니다. 내 레이아웃은 기본적으로gestureListener가 onTouch가 호출 될 때 null입니다.

부모 : LinearLayout을

아이 :의 GridView

I 전체 화면의 gestureListener을 구현하기 위해 노력하고있어,하지만 난있는 gridview가에라는있는 LinearLayout에 대한 gestureListener을 할 경우, 그것은 아무튼 등록하지 마라. 그래서 나는 그것들을 별도로해야만한다. 내가 그렇게 할 때, 나는 여기에 코드 여기

//set swipe listener for the calendar view 
     calendar.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View arg0, MotionEvent event) { 
       // TODO Auto-generated method stub 
       return gestures.onTouchEvent(event); 
      } 
     }); 

     //set swipe listener for rest of the view 
     mainView.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View arg0, MotionEvent event) { 
       // TODO Auto-generated method stub 
       return gestures.onTouchEvent(event); 
      } 
     }); 

      @Override 
     public boolean onFling(MotionEvent firstAction, MotionEvent secondAction, float velocityX, 
       float velocityY) { 
      //change the calendar images based on what the user flings. If they fling in between certain 
      //months, change the background. 

      Toast.makeText(CalendarDisplay.this, Float.toString(firstAction.getX()) + " : " 
        + Float.toString(secondAction.getX()), Toast.LENGTH_SHORT).show(); 
      //right to left fling 
      if (firstAction.getX() - secondAction.getX() > 120 && Math.abs(velocityX) > 200) { 
       mainView.setBackgroundDrawable(getResources().getDrawable(R.drawable.spring_bkgr)); 
       month -= 1; 
       if (month < 0) { 
        month = 11; 
        year -= 1; 
        myCal.set(Calendar.YEAR, year); 
        myCal.set(Calendar.MONTH, month); 
        adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay); 
        calendar.setAdapter(adapter); 
        currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis())); 
       } 
       //sets the new month and resets the adapter 
       myCal.set(Calendar.MONTH, month); 
       adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay); 
       calendar.setAdapter(adapter); 
       currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis())); 

       setBackGrounds(); 

      } 
      //left to right fling 
      else if (secondAction.getX() - firstAction.getX() > 120 && Math.abs(velocityX) > 200) { 
       mainView.setBackgroundDrawable(getResources().getDrawable(R.drawable.summer_bkgr)); 
       month += 1; 
       if (month > 11) { 
        month = 0; 
        year += 1; 
        myCal.set(Calendar.YEAR, year); 
        myCal.set(Calendar.MONTH, month); 
        adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay); 
        calendar.setAdapter(adapter); 
        currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis())); 
       } 
       //probably inefficient but...yeah it works. 
       myCal.set(Calendar.MONTH, month); 
       adapter = new CalendarAdapter(CalendarDisplay.this, year, month, currentDay); 
       calendar.setAdapter(adapter); 
       currentDate.setText(DateFormat.format(dateTemplate, myCal.getTimeInMillis())); 

       setBackGrounds(); 
      } 

      return true; 
     } 

는 로그 캣이된다 있습니다 .. GRIDVIEW의 firstAction gestureListener에 널 포인터를 얻을

05-18 17:41:53.585: E/AndroidRuntime(23793): FATAL EXCEPTION: main 
05-18 17:41:53.585: E/AndroidRuntime(23793): java.lang.NullPointerException 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.calendar.CalendarDisplay$myGestureListener.onFling(CalendarDisplay.java:223) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:606) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.calendar.CalendarDisplay$1.onTouch(CalendarDisplay.java:95) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.View.dispatchTouchEvent(View.java:3881) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1930) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1205) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.app.Activity.dispatchTouchEvent(Activity.java:2155) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2249) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.view.ViewRoot.handleMessage(ViewRoot.java:1933) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.os.Looper.loop(Looper.java:130) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at android.app.ActivityThread.main(ActivityThread.java:3906) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at java.lang.reflect.Method.invokeNative(Native Method) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at java.lang.reflect.Method.invoke(Method.java:507) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598) 
05-18 17:41:53.585: E/AndroidRuntime(23793): at dalvik.system.NativeStart.main(Native Method) 
+0

logcat은 무엇을 말하고 있습니까? – pskink

+0

onTouch 호출에서 null 포인터를 사용한 다음 onFling에서 firstAction 또는 secondAction의 첫 번째 인스턴스를 가리 킵니다. 나는 그것이 gridView이기 때문에 자신의 gestureListeners가 필요한 각각의보기가 부 풀린다는 사실과 관련이 있는지 궁금합니다. ??? – cj1098

+0

onTouch는 gestures.onTouchEvent (event) 만 호출합니다. 그래서 NPE의 이유는 매우 명확합니다. 몸짓은 null – pskink

답변

2

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     page= (TestLayout) findViewById(R.id.mainpage); 
     page.setOnTouchListener(this); 

    } 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     onStop(); 
    } 
    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       //if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          page.pre(); 
         } else { 
          page.next(); 
         } 
        } 
       //} 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 

시도 page.pre()page.post() 메서드를 비즈니스 논리로 변경하십시오.

희망이 있습니다.

+0

죄송합니다. 내 코드와 많이 다르지 않습니다. ( 고마워! – cj1098

관련 문제