调用onTouch时,gestureListener为null

时间:2013-05-18 06:52:55

标签: android android-gesture

由于某种原因,当用户从左向右翻动时,我的应用程序第一次崩溃。但是,如果他们做左右投掷的权利,那就行得很好。 它只在我构建的gridView上执行此操作。我的布局基本上是

父级:LinearLayout

Child:GridView

我正在尝试为整个屏幕实现gestureListener,但如果我为gridView所在的linearLayout执行gestureListener,则它不会注册。所以我必须分开做。当我这样做时,我在gridView的firstAction 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;
        }

这是logcat

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)

1 个答案:

答案 0 :(得分: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()

希望它有所帮助。