cj1098
cj1098

Reputation: 1610

gestureListener is null when onTouch is called

for some reason when the user flings from left to right my app crashes for the first time. But if they do a right to left fling it works fine. It only does this on a gridView that I've constructed. My layout is basically

Parent: LinearLayout

Child: GridView

I'm trying to implement the gestureListener for the entire screen, but if I do gestureListener for the linearLayout that gridView is in, it doesn't register. So I have to do them seperately. When I do that, I get a null pointer on the gridView's firstAction gestureListener.. Here is the code

//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;
        }

Here is the 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)

Upvotes: 0

Views: 720

Answers (1)

Shubham
Shubham

Reputation: 1442

Try this

 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;
        }

Change the method page.pre() and page.post() with your business logic.

Hope it helps.

Upvotes: 2

Related Questions