Recycler在滚动时查看nullpointer异常

时间:2015-04-16 13:41:06

标签: java android android-recyclerview

我有自定义纹理视图。我有一个循环器视图,循环器视图中的每个单元格都有我上面提到的纹理视图。我想要实现的是在纹理视图中播放视频。只有顶部可见纹理视图才能开始播放。但是,当第一个项完全不可见且我没有触摸显示时,我编写的代码会抛出空指针异常。我的代码如下所示:

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            mPrevTopItemPos = mCurrentTopItemPos;
            mCurrentTopItemPos = mLayoutManager.findFirstVisibleItemPosition();



            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                VSUtility.log("ScrollSTate:", "Idle");
                ViewGroup parent = (ViewGroup) mRecyclerView.getChildAt(mCurrentTopItemPos);
                FrameLayout videoFrame = (FrameLayout) parent.findViewById(R.id.surface_container);

                if (videoFrame != null && VSUtility.isViewVisible(videoFrame, mRecyclerView)) {
                    VSUtility.displayToastMessage(getActivity(), "frame  visible :" + mCurrentTopItemPos);
                } else {
                    VSUtility.log("INVISIBLE:", "Idle" + mCurrentTopItemPos);
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            paginateIfNecessary(recyclerView);


            /*int itemAtTop = -1;
            Rect visibleRect = new Rect();
            itemAtTop = mLayoutManager.findFirstVisibleItemPosition();
            mPrevItemInList = itemAtTop;
            *//*VSCustomNearbyItemAdapter.ViewHolder holder = (VSCustomNearbyItemAdapter.ViewHolder)
                    mRecyclerView.getChildViewHolder(mRecyclerView.getChildAt(itemAtTop));*//*
            FrameLayout frameLayout = (FrameLayout) mRecyclerView.getChildAt(itemAtTop)
                    .findViewById(R.id.surface_container);
            //textureView.getWindowVisibleDisplayFrame(visibleRect);
            if (isViewVisible(frameLayout)) {
                if (mPrevItemInList > -1) {

                }
                SurfaceView sView = (SurfaceView) frameLayout.findViewById(R.id.surface_video_feeds);
                ProgressBar progress = (ProgressBar) frameLayout.findViewById(R.id.progress_circular_video);
                progress.setVisibility(View.VISIBLE);

                VSUtility.log("Visiblity", "Visible");
            } else {
                VSUtility.log("Visiblity", "Invisible");
            }*/

        }
    });

错误日志是:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.findViewById(int)' on a null object reference
        at fragments.VSNearbyFragment$1.onScrollStateChanged(VSNearbyFragment.java:184)
        at android.support.v7.widget.RecyclerView.setScrollState(RecyclerView.java:823)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:1704)
        at android.view.View.dispatchTouchEvent(View.java:8388)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
        at android.view.View.dispatchPointerEvent(View.java:8578)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
        at android.view.InputEventReceiver.dispatchInputEv

崩溃发生在以下行:

   FrameLayout videoFrame = (FrameLayout) parent.findViewById(R.id.surface_container);

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该使用的方法不是getChildAt。使用RecyclerView#findViewHolderForAdapterPosition

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)