高手解答!Fragment布局中的View概率性空指针

Android 码拜 9年前 (2016-05-01) 1800次浏览
本人用Viewpager+Fragment布局。
Viewpager和Fragment都是在Activity的onCreate初始化的。
用的是FragmentPagerAdapter,并且mViewPager.setOffscreenPageLimit(3)保证Fragment不会回收。
当程序最小化后,再恢复,此时对某一个Fragment布局中的View执行rotate属性动画会概率性的报出空指针异常。
java.lang.NullPointerException
at com.mars.note.fragment.RecentRecordsFragment.runDrawerAnim(RecentRecordsFragment.java:1408)
at com.mars.note.Main$1.onDrawerSlide(Main.java:102)
at android.support.v4.widget.DrawerLayout.dispatchOnDrawerSlide(DrawerLayout.java:607)
at android.support.v4.widget.DrawerLayout.setDrawerViewOffset(DrawerLayout.java:618)
at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewPositionChanged(DrawerLayout.java:1510)
at android.support.v4.widget.ViewDragHelper.dragTo(ViewDragHelper.java:1382)
at android.support.v4.widget.ViewDragHelper.processTouchEvent(ViewDragHelper.java:1117)
at android.support.v4.widget.DrawerLayout.onTouchEvent(DrawerLayout.java:1021)
at android.view.View.dispatchTouchEvent(View.java:7714)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2473)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7894)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3986)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3865)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3552)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3453)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3609)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3453)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3426)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5571)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5551)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5522)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5651)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5624)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:542)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
at dalvik.system.NativeStart.main(Native Method)
1408这一行是
public void runDrawerAnim(float arg0){
overflow_options.setRotation(arg0);
}

调用函数是
@Override
public void onDrawerSlide(View arg0, float arg1) {
mRecentRecordsFragment.runDrawerAnim(arg1 * 360);
}
这个bug是概率性的,大致运气好的话十分之一。
高手们帮帮忙!

解决方案

50

空指针问题,overflow_options可能是空,你把fragement的生命周期的各个点打上断点,看看第二次都执行了那,估计overflow_options没有被初始化

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明高手解答!Fragment布局中的View概率性空指针
喜欢 (0)
[1034331897@qq.com]
分享 (0)