ScrollGalleryView icon indicating copy to clipboard operation
ScrollGalleryView copied to clipboard

Nullpointer in HackyViewPager

Open wurstnudl opened this issue 10 years ago • 7 comments

Hi,

this behaviour is not always reproducable. Happens during device rotation, currently on a Genymotion Nexus 7 with android 4.1.1.

Let me know if you need more info. Not sure why the viewpager is null here.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.veinhorn.scrollgalleryview.HackyViewPager.setLocked(boolean)' on a null object reference at com.veinhorn.scrollgalleryview.ImageFragment.onCreateView(ImageFragment.java:41) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046) at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:174) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:598) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) at android.app.Activity.performStart(Activity.java:6268)

wurstnudl avatar Mar 16 '16 15:03 wurstnudl

Hi,

the problem was somehow that the ImageFragments are being retained which should have been destroyed. Didn't really find out why this happens, what helps as workaround though is the following (it's the onDestroy of the fragment I'm using the scrollGallery in):

List<Fragment> fragments = supportFragmentManager.getFragments(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); for (Fragment currentFragment : fragments) { if (currentFragment instanceof ImageFragment) { fragmentTransaction.remove(currentFragment); } } if (fragmentTransaction.commit() < 0) { // don't care. basically means that those fragments have no backstack entries }

wurstnudl avatar Mar 17 '16 08:03 wurstnudl

@wurstnudl Much thanks for your research. :+1: I'll try to fix this for the next release.

VEINHORN avatar Mar 17 '16 15:03 VEINHORN

@VEINHORN After some more research (onDestroy seems not to be the right place for this peace of code), I ran across the following SDK bug. It's marked as fixed, but I think it's not...and I'm not the only one.

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

wurstnudl avatar Mar 21 '16 08:03 wurstnudl

@VEINHORN Actually the problem is a lot simpler than thought. I think the fragments of the view pager are destroyed when an activity is destroyed, you can leave that handling to android probably. You could add nullchecks to the ImageFragment on lines 41 and 42 for the viewPager and the backgroundImage. I was able to fix it that way for now, I set my own patched slide adapter via reflection which instantiates a patched ImageFragment with those nullchecks and everything works fine.

Thanks for now.

wurstnudl avatar Mar 21 '16 13:03 wurstnudl

@wurstnudl can you post more information for how to fix this bug?

ronanlima avatar Jan 29 '19 21:01 ronanlima

Any fix for this... I can recreate it everytime. Whenever i try to swipe back or select a previous image the following error is thrown.

Attempt to invoke virtual method 'void com.veinhorn.scrollgalleryview.HackyViewPager.setLocked(boolean)' on a null object reference

nidhinprathap avatar May 07 '20 12:05 nidhinprathap

@nidhinprathap Hi. Which version of library do you use?

VEINHORN avatar May 08 '20 08:05 VEINHORN