androidandroid-fragmentsandroid-viewflipper

GestorDetector in Fragment not working


I am very new to Android development I am trying to convert an activity into a fragment, it worked fine in an Activity besides being an old tutorial I went through. I have worked out some of the bugs and I am sure there are more things i need to straighten up so any additional tips are welcome.

Here is the code and my log is below as well. I am just using a navigationView in my MainActivity to launch this fragment.

categorySelectFragment.java

public class categorySelectorFragment extends Fragment {

private ViewFlipper mViewFlipper;
private int mSpeed;
private int mCount;
private int mFactor;
private boolean mAnimating;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.category_selector, container, false);

    mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper);
    //mDetector = new GestureDetector(getActivity(), this);
    mAnimating = false;
    mCount = 0;
    mSpeed = 0;

    final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return true;
        }

        private Runnable r1 = new Runnable() {

            @Override
            public void run() {
                up();
                if (mCount<1) {
                    mAnimating = false;
                } else {
                    Handler h = new Handler();
                    h.postDelayed(r1, mSpeed);
                }
            }

        };

        private Runnable r2 = new Runnable() {

            @Override
            public void run() {
                down();
                if (mCount<1) {
                    mAnimating = false;
                } else {
                    Handler h = new Handler();
                    h.postDelayed(r2, mSpeed);
                }
            }

        };

        @Override
        public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
            try {
                if (mAnimating) return true;
                mAnimating = true;
                mCount = (int) Math.abs(yVelocity) / 300;
                mFactor = (int) 300 / mCount;
                mSpeed = mFactor;
                if (yVelocity>0) {
                    //down
                    Handler h = new Handler();
                    h.postDelayed(r2, mSpeed);
                } else {
                    //up
                    Handler h = new Handler();
                    h.postDelayed(r1, mSpeed);
                }
                ((TextView)getActivity().findViewById(R.id.velocity)).setText("VELOCITY => "+Float.toString(yVelocity));
            } catch (ArithmeticException e) {
                //swiped too slow doesn't register
                mAnimating = false;
            }
            return true;
        }

        private void up() {
            mCount--;
            mSpeed+=mFactor;
            Animation inFromBottom = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromBottom.setInterpolator(new AccelerateInterpolator());
            inFromBottom.setDuration(mSpeed);
            Animation outToTop = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, -1.0f);
            outToTop.setInterpolator(new AccelerateInterpolator());
            outToTop.setDuration(mSpeed);
            mViewFlipper.clearAnimation();
            mViewFlipper.setInAnimation(inFromBottom);
            mViewFlipper.setOutAnimation(outToTop);
            if (mViewFlipper.getDisplayedChild()==0) {
                mViewFlipper.setDisplayedChild(2);
            } else {
                mViewFlipper.showPrevious();
            }
            ((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
            ((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
        }

        private void down() {
            mCount--;
            mSpeed+=mFactor;
            Animation outToBottom = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 1.0f);
            outToBottom.setInterpolator(new AccelerateInterpolator());
            outToBottom.setDuration(mSpeed);
            Animation inFromTop = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, -1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromTop.setInterpolator(new AccelerateInterpolator());
            inFromTop.setDuration(mSpeed);
            mViewFlipper.clearAnimation();
            mViewFlipper.setInAnimation(inFromTop);
            mViewFlipper.setOutAnimation(outToBottom);
            if (mViewFlipper.getDisplayedChild()==0) {
                mViewFlipper.setDisplayedChild(2);
            } else {
                mViewFlipper.showPrevious();
            }
            ((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => "+Integer.toString(mCount));
            ((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => "+Integer.toString(mSpeed));
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }

        public void onSwipeRight() {
            Toast.makeText(getActivity(), " RIGHT ", Toast.LENGTH_LONG).show();
        }

        public void onSwipeLeft() {
            Toast.makeText(getActivity(), " LEFT ", Toast.LENGTH_LONG).show();


        }

        public void onSwipeTop() {
            Toast.makeText(getActivity(), " TOP ", Toast.LENGTH_LONG).show();

        }

        public void onSwipeBottom() {
            Toast.makeText(getActivity(), " BOTTOM ", Toast.LENGTH_LONG).show();

        }


    });


    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    });

    return view;


}

}

Logcat

FATAL EXCEPTION: main
Process: com.example.game, PID: 9403

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.clearAnimation()' on a null object reference

at com.example.game.categorySelectorFragment$1.up(categorySelectorFragment.java:122)
at com.example.game.categorySelectorFragment$1.access$000(categorySelectorFragment.java:42)
at com.example.game.categorySelectorFragment$1$1.run(categorySelectorFragment.java:54)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Thanks for the help!


Solution

  • 1)Try replacing mViewFlipper = (ViewFlipper)getActivity().findViewById(R.id.view_flipper); with mViewFlipper = (ViewFlipper)view.findViewById(R.id.view_flipper);

    2) I suggest you to initialize views in onActivityCreated() method.