-1

I am working on a simple android application which has a ViewPager with swiping images. I have static application containing 12 images. When I swipe, the app is getting crashed after the fourth image.

How can I understand the stacktrace and solve this problem?

e.. 05-24 21:37:25.718: E/AndroidRuntime(551): FATAL EXCEPTION: main 05-24 21:37:25.718: E/AndroidRuntime(551):

java.lang.OutOfMemoryError 05-24 21:37:25.718: E/AndroidRuntime(551): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.content.res.Resources.loadDrawable(Resources.java:1935) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.content.res.Resources.getDrawable(Resources.java:664) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.widget.ImageView.resolveUri(ImageView.java:542) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.widget.ImageView.setImageResource(ImageView.java:315) 05-24 21:37:25.718: E/AndroidRuntime(551): at com.sqisland.android.swipe_image_viewer.MainActivity$ImagePagerAdapter.instantiateItem(MainActivity.java:72) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.support.v4.view.ViewPager.populate(ViewPager.java:875) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.support.v4.view.ViewPager.populate(ViewPager.java:772) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.drawChild(ViewGroup.java:2729) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.View.draw(View.java:10981) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.widget.FrameLayout.draw(FrameLayout.java:450) 05-24 21:37:25.718: E/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:137) 05-24 21:37:25.718: E/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-24 21:37:25.718: E/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method) 05-24 21:37:25.718: E/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:511) 05-24 21:37:25.718: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-24 21:37:25.718: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-24 21:37:25.718: E/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
user1511208
  • 67
  • 1
  • 3
  • 9

3 Answers3

0

It seems you are using image with very large size that the device can not handle. Try using some smaller sizes

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

ViewPager adapter loads all your images at a time that's why you got the error. So what are the actual reason: 1. Your image size is very large 2. You did not use image resources properly. Try to put your different size image in different folder like ldpi, hdpi, xdpi folder. Try to use the above tricks. If you still get error, you can follow this:

  1. Change the FragmentPagerAdapter to a FragmentStatePagerAdapter. The reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current and traversable views.

  2. Override the adapter method getItemPosition (shown below). When we call mAdapter.notifyDataSetChanged(); the ViewPager interrogates the adapter to determine what has changed in terms of positioning. We use this method to say that everything has changed so reprocess all your view positioning. Actual answer is here: Remove Fragment Page from ViewPager in Android

Community
  • 1
  • 1
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
0

I did change the FragmentPagerAdapter to a FragmentStatePagerAdapter. However the swiping motion is quite sluggish. Seems like it solved the problem for the time being.

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */

The detail tutorial is available here http://blog.kerul.net/2013/09/adding-xml-layout-in-swipe-views.html