ViewPager上的SetCurrentItem导致异常

时间:2015-08-19 00:29:52

标签: android android-fragments android-viewpager

我有一个奇怪的问题,但在过去三个小时内无法解决这个问题。所以我需要帮助。我正在创建一个类似Play Music中的PlayerUI。 Activity具有ViewPager并根据PlayLists中的曲目准备片段列表,并允许用户使用片段UI上的滑动或“下一个”和“上一个”按钮从一个轨道切换到另一个轨道。

滑动功能一切正常。在我的上一个和下一个按钮中,我在viewPager上调用setCurrentItem,并根据来自viewPager的getCurrentItem的值递增或递减计数。问题是下一个工作正常但是当我在最后一个项目之前点击项目的下一个时抛出异常。与之前的按钮相同。它工作得很好但是当我点击第二个片段上的前一个时它会引发异常。

这是我的错误日志::

java.lang.NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3366)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

以下是我的Activity的代码,片段是我在视图分页器上设置的片段列表

ViewPagerAdapter adapter = new       ViewPagerAdapter(getSupportFragmentManager(),fragments);
adapter.setTrackList(trackList);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
viewPager.setCurrentItem(currentTrack.getPosition());

当用户点击Next和Previous按钮(在片段中)时,我只需执行以下操作之一

((PlayerActivity)getActivity()).viewPager.setCurrentItem(((PlayerActivity)getActivity()).viewPager.getCurrentItem()+1);

((PlayerActivity)getActivity()).viewPager.setCurrentItem(((PlayerActivity)getActivity()).viewPager.getCurrentItem()-1);

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

Runnable dirtyHack = new Runnable() {
        @Override
        public void run() {
            pager.setCurrentItem(2, 0);
        }
    };
    Handler handler = new Handler();
    handler.postDelayed(dirtyHack, 100);

...

可能布局还没有膨胀,所以我必须等待或获得NPE。请参阅以下链接

https://code.google.com/p/android/issues/detail?id=75309

Android ViewPager setCurrentItem not working after onResume

setCurrentItem after setting adapter on GridViewPager return NPE

答案 1 :(得分:0)

您收到此错误是因为您在ViewPager中超出了多个片段的范围,当您在最后一个项目中并且点击下一个按钮时,您应该设置currentItem(0)