如何解决Android View Pager内存异常?

时间:2015-02-09 05:35:06

标签: android android-viewpager out-of-memory

我使用android auto scroll view pager进行无限滚动。我有四个片段作为视图寻呼机元素。前三个包含一个Imageview,最后一个包含一个Listview。片段中的每个图像具有大约260KB的大小。在运行此页面大约5分钟时,我出现了内存异常,从而导致异常膨胀。

我添加了android:largeHeap="true" 在清单中。我尝试了FragmentPagerAdapterFragmentStatePagerAdapter。任何人都可以建议我还应该做些什么?有什么我必须避免的吗?

Logcat:

02-05 22:24:58.649: D/dalvikvm(2006): GC_BEFORE_OOM freed 12K, 18% free 40067K/48460K, paused 75ms, total 75ms
02-05 22:24:58.649: E/dalvikvm-heap(2006): Out of memory on a 1030552-byte allocation.
02-05 22:24:58.659: I/dalvikvm(2006): "main" prio=5 tid=1 RUNNABLE
02-05 22:24:58.659: I/dalvikvm(2006):   | group="main" sCount=0 dsCount=0 obj=0x40c959a0 self=0x4521b010
02-05 22:24:58.659: I/dalvikvm(2006):   | sysTid=2006 nice=0 sched=0/0 cgrp=[no-cpu-subsys] handle=1074267100
02-05 22:24:58.659: I/dalvikvm(2006):   | state=R schedstat=( 66536119633 40132743333 68805 ) utm=6247 stm=406 core=0
02-05 22:24:58.659: I/dalvikvm(2006):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-05 22:24:58.669: I/dalvikvm(2006):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-05 22:24:58.679: I/dalvikvm(2006):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-05 22:24:58.679: I/dalvikvm(2006):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
02-05 22:24:58.679: I/dalvikvm(2006):   at android.content.res.Resources.loadDrawable(Resources.java:1965)
02-05 22:24:58.689: I/dalvikvm(2006):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-05 22:24:58.689: I/dalvikvm(2006):   at android.widget.ImageView.<init>(ImageView.java:120)
02-05 22:24:58.689: I/dalvikvm(2006):   at android.widget.ImageView.<init>(ImageView.java:110)
02-05 22:24:58.689: I/dalvikvm(2006):   at java.lang.reflect.Constructor.constructNative(Native Method)
02-05 22:24:58.689: I/dalvikvm(2006):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-05 22:24:58.699: I/dalvikvm(2006):   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
02-05 22:24:58.709: I/dalvikvm(2006):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-05 22:24:58.709: I/dalvikvm(2006):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
02-05 22:24:58.709: I/dalvikvm(2006):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-05 22:24:58.709: I/dalvikvm(2006):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-05 22:24:58.709: I/dalvikvm(2006):   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-05 22:24:58.709: I/dalvikvm(2006):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-05 22:24:58.719: I/dalvikvm(2006):   at com.example.test.MFirstFragment.onCreateView(WelcomeFragment1.java:29)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1240)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:612)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:397)
02-05 22:24:58.719: I/dalvikvm(2006):   at cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager.scrollOnce(AutoScrollViewPager.java:169)
02-05 22:24:58.719: I/dalvikvm(2006):   at cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager$MyHandler.handleMessage(AutoScrollViewPager.java:236)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.os.Looper.loop(Looper.java:137)
02-05 22:24:58.719: I/dalvikvm(2006):   at android.app.ActivityThread.main(ActivityThread.java:5041)
02-05 22:24:58.719: I/dalvikvm(2006):   at java.lang.reflect.Method.invokeNative(Native Method)
02-05 22:24:58.719: I/dalvikvm(2006):   at java.lang.reflect.Method.invoke(Method.java:511)
02-05 22:24:58.719: I/dalvikvm(2006):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-05 22:24:58.719: I/dalvikvm(2006):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-05 22:24:58.719: I/dalvikvm(2006):   at dalvik.system.NativeStart.main(Native Method)
02-05 22:24:58.739: D/skia(2006): --- decoder->decode returned false
02-05 22:24:58.739: D/AndroidRuntime(2006): Shutting down VM
02-05 22:24:58.739: W/dalvikvm(2006): threadid=1: thread exiting with uncaught exception (group=0x40c94930)
02-05 22:24:59.049: E/AndroidRuntime(2006): FATAL EXCEPTION: main
02-05 22:24:59.049: E/AndroidRuntime(2006): android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
02-05 22:24:59.049: E/AndroidRuntime(2006):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
02-05 22:24:59.049: E/AndroidRuntime(2006):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-05 22:24:59.049: E/AndroidRuntime(2006):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)

3 个答案:

答案 0 :(得分:3)

首先,不要在第一次使用android:largeHeap =“true”。在用完所有其他方法后,它可能被用作最后的手段。

那么出路是什么?我从中学到的教训是,“不要重新使用轮子”。使用PicassoFrescoGlide等第三方库来摆脱这种情况。额外的财富是你的代码是可维护的。例如,Glide可以快速压缩图像并将图像加载到ImageView中。它的API,真的很整洁。

答案 1 :(得分:0)

在pageradapter(或任何页面适配器)中添加onDestroy方法,以便您以前的图像视图将用于垃圾收集器和新内存的可用内存。

@Override
public void destroyItem(View collection, int position, Object o) {
    Log.d("main", "garbaging imageview at: " + position);
    View view = (View) o;
    ((ViewPager) collection).removeView(view);
    view = null;
}

答案 2 :(得分:0)

尝试在应用程序内部放置清单android:largeHeap =“true”

它解决了大部分内存问题