Hello! I am making a calendar app. The calendar is a griedview and i want to set onFling fuction on the gridview, so when the user swipe from left to right it ill generate a new view with the next month. I am able to set that onFling on other elements in the same view and worked fine but when i try to do the same n my gridView i get an error (Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference). I tried to put the listener on the entire view and work on all elements and layout but i keep getting that error when the user is trying to swipe on the gridview. Any suggestions?? ThanksHere is part of my code.
Java:
public class CalendarView extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
final GestureDetector gdt = new GestureDetector(new GestureListener());
GridView grid=(GridView)findViewById(R.id.gridview);
grid.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
gdt.onTouchEvent(event);
return true;
}
});
}
private class GestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX)
> SWIPE_THRESHOLD_VELOCITY) {
if(month.get(Calendar.MONTH)== month.getActualMaximum(Calendar.MONTH)) {
month.set((month.get(Calendar.YEAR)+1),month.getActualMinimum(Calendar.MONTH),1);
} else {
month.set(Calendar.MONTH,month.get(Calendar.MONTH)+1);
}
refreshCalendar();
return true; // Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(month.get(Calendar.MONTH)== month.getActualMinimum(Calendar.MONTH)) {
month.set((month.get(Calendar.YEAR)-1),month.getActualMaximum(Calendar.MONTH),1);
} else {
month.set(Calendar.MONTH,month.get(Calendar.MONTH)-1);
}
refreshCalendar();
return true; // Left to right
}
return false;
}