2014-09-25 96 views
0

我有一個ViewPager顯示新聞條目的詳細信息。你可以在30個條目之間來回滑動。這些條目中的每一個都是保存ImageView的View。該ImageView使用Picasso圖像庫從URL填充圖像。Android的ViewPager與片段控股ImageView

它工作正常,但有時當我幾乎經歷了所有的各個項目(片段)我的應用程序突然崩潰,輕掃收到此錯誤:

Caused by: java.lang.OutOfMemoryError 
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594) 
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429) 
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) 
at android.content.res.Resources.loadDrawable(Resources.java:2208) 
at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
at android.widget.ImageView.<init>(ImageView.java:129) 
at android.widget.ImageView.<init>(ImageView.java:119) 
at java.lang.reflect.Constructor.constructNative(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at com.app.mobil.fragment.placeholder.FanpicPlaceholderFragment.onCreateView(FanpicPlaceholderFragment.java:92) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:919) 
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
at android.view.Choreographer.doFrame(Choreographer.java:543) 
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:5139) 
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:796) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
at dalvik.system.NativeStart.main(Native Method) 

而且刷卡時我有時會收到下面的logcat消息(噹噹前視圖中包含加載到ImageView的大圖像):

09-25 13:19:59.816 31859-32208/com.app.mobile E/dalvikvm-heap﹕ Out of memory on a 3276816-byte allocation. 

我對整個事情的一個問題:我與畢加索裝入ImageViews的圖像有時是相當大的(〜每幅圖像3MB)。

有人可以向我解釋我做錯了什麼嗎?

+0

你使用什麼樣的適配器爲你的ViewPager? – tasomaniac 2014-09-25 11:28:33

+0

試試這個http://stackoverflow.com/questions/22256097/out-of-memory-error-when-using-animationdrawable/22256342#22256342 – 2014-09-25 11:31:01

+0

請改變使用圖像加載器庫可能是影響somting – 2014-09-25 11:32:36

回答

1

試試這個:

android:largeHeap="true" 

在你的清單,在應用程序標籤

+0

感謝您提供最簡單的解決方案對我的問題。 – Klaus 2014-09-25 12:06:15

0

變換圖像轉換爲佈局使用,以減少內存大小下面的代碼 -

Picasso.with(上下文).load (url).resize(width,height).into(imageView)

確保您使用的是畢加索的libr ary with version - 2.3.4

+0

我從2.3.2升級到2.3.4。還是一樣的錯誤。這就是我如何加載我的圖片: 畢加索 \t。隨着(上下文) \t .load(URL) \t .resize(1024,800) \t .centerCrop() \t .into(fanpicImageView); – Klaus 2014-09-25 12:01:11

+0

嘗試縮小圖像的寬度和高度。例如width = 100和height = 50。 (1024,800)似乎非常高,並且會消耗更多的內存,從而導致內存不足錯誤。按照您的要求嘗試一些最小值。 – 2014-09-25 12:18:51