Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' On Exoplayer

[亡魂溺海] 提交于 2020-08-10 19:11:11

问题


I read multiple question on same issue but nothing helps in my case.

Here when i implement SimpleOnGestureListener with onScroll on empty layout i can detect scroll up, down, right, left. but when i try same on ExoPlayer i get error saying

java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference at Activities.VideoPlayerActivity$3.onScroll(VideoPlayerActivity.java:174) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:624) at Activities.VideoPlayerActivity.onTouchEvent(VideoPlayerActivity.java:221)

And here is line 174 float x1 = e1.getX(); and 221 mGestureDetector.onTouchEvent(event);

Any help why am i not able to detect scroll on ExoPlayer where else i can detect on empty layout.

here is code :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        ButterKnife.bind(this);

        addTouchListener();
        playVideo();
    }

    private void playVideo() {
        try {
            //play
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void addTouchListener() {
        GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                float x1 = e1.getX();
                float y1 = e1.getY();
                float x2 = e2.getX();
                float y2 = e2.getY();
                float absX = Math.abs(x1 - x2);
                float absY = Math.abs(y1 - y2);

                float absDistanceX = Math.abs(distanceX);
                float absDistanceY = Math.abs(distanceY);

                if (absDistanceX < absDistanceY && !mIntoSeek) {
                    if (distanceY > 0) {
                        if (x1 >= mScreenWidth * SLIP_SENSITIVITY) {
                            Toast.makeText(VideoPlayerActivity.this, "right up",Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(VideoPlayerActivity.this, "left up",Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        if (x1 >= mScreenWidth * SLIP_SENSITIVITY) {
                            Toast.makeText(VideoPlayerActivity.this, "right down",Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(VideoPlayerActivity.this, "left down",Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    if (absX > absY) {
                        mIntoSeek = true;
                        //onSeekChange(x1, x2);
                        return true;
                    }
                }
                return false;
            }
        };
        mGestureDetector = new GestureDetector(this, mSimpleOnGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            mGestureDetector.onTouchEvent(event);
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
        }
        return super.onTouchEvent(event);
    }
}

来源:https://stackoverflow.com/questions/62507726/attempt-to-invoke-virtual-method-float-android-view-motionevent-getx-on-exop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!