何らかの理由で、ユーザーが左から右に飛んでいくと、アプリが初めてクラッシュします。しかし、彼らが右から左へのフリングを行うと、うまく機能します。これは、私が構築した gridView でのみ行われます。私のレイアウトは基本的に
親: LinearLayout
子: GridView
画面全体にgestureListenerを実装しようとしていますが、gridViewが入っているlinearLayoutにgestureListenerを実装すると登録されません。だから私はそれらを別々にやらなければなりません。これを行うと、gridView の firstAction ジェスチャリスナーで null ポインターが取得されます。コードは次のとおりです。
//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)