出于某种原因,当用户从左向右移动时,我的应用程序第一次崩溃了。但是,如果他们从右向左投掷,则效果很好。它只在我构建的 gridView 上执行此操作。我的布局基本上是
父:线性布局
孩子:GridView
我正在尝试为整个屏幕实现gestureListener,但是如果我为gridView 所在的linearLayout 执行gestureListener,它不会注册。所以我必须分开做。当我这样做时,我在 gridView 的 firstAction 手势监听器上得到一个空指针。这是代码
//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;
}
这是日志猫
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)