時,我已經試過這樣: OutOfMemoryError的OutOfMemoryError加載動畫
而且看了很多其他類似的帖子,但沒有一次成功
這是錯誤:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lud.root.jetfighter, PID: 19789
java.lang.OutOfMemoryError: Failed to allocate a 4940652 byte allocation with 2168368 free bytes and 2MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1085)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2879)
at android.content.res.Resources.loadDrawable(Resources.java:2768)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:324)
at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:294)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1220)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1129)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2874)
at android.content.res.Resources.loadDrawable(Resources.java:2768)
at android.content.res.Resources.getDrawable(Resources.java:932)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
at android.content.Context.getDrawable(Context.java:465)
at android.view.View.setBackgroundResource(View.java:17566)
at android.support.v7.widget.AppCompatImageView.setBackgroundResource(AppCompatImageView.java:76)
at com.lud.root.jetfighter.Instruction.onCreate(Instruction.java:32)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I/System: FinalizerDaemon: finalize objects = 381
閱讀一些其他職位我來了解的超大內存限制的大位圖,但文件太小,我認爲超出限制(他們是8至12kB最大.png文件)
我已經閱讀了很多關於位圖問題的帖子,但沒有人可以幫助我。
我嘗試這樣做: android:largeHeap="true"
但由於它沒有工作,我讀了另一篇文章,並建議將其刪除,所以我做到了。
我Instuction.java文件,該文件運行到錯誤:
setContentView(R.layout.activity_instruction);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ll = (LinearLayout)findViewById(R.id.instruction);
ll.setOnClickListener(this);
imv1 = (ImageView) findViewById(R.id.instruction_imv1);
imv3 = (ImageView) findViewById(R.id.instruction_imv3);
imv1.setBackgroundResource(R.drawable.inst); // **This is where error occurs**
imv3.setBackgroundResource(R.drawable.collision);
tap = (AnimationDrawable) imv1.getBackground();
tap.start();
collision = (AnimationDrawable) imv3.getBackground();
collision.start();
我裝22個文件爲研究所動畫。
這是我inst.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/enlarge001" android:duration="100" />
<item android:drawable="@drawable/enlarge002" android:duration="100" />
<item android:drawable="@drawable/enlarge003" android:duration="100" />
<item android:drawable="@drawable/enlarge005" android:duration="100" />
<item android:drawable="@drawable/enlarge006" android:duration="100" />
<item android:drawable="@drawable/enlarge007" android:duration="100" />
<item android:drawable="@drawable/enlarge008" android:duration="100" />
<item android:drawable="@drawable/enlarge009" android:duration="100" />
<item android:drawable="@drawable/enlarge010" android:duration="100" />
<item android:drawable="@drawable/enlarge011" android:duration="100" />
<item android:drawable="@drawable/enlarge012" android:duration="100" />
<item android:drawable="@drawable/enlarge013" android:duration="100" />
<item android:drawable="@drawable/enlarge014" android:duration="100" />
<item android:drawable="@drawable/enlarge015" android:duration="100" />
<item android:drawable="@drawable/enlarge016" android:duration="100" />
<item android:drawable="@drawable/enlarge017" android:duration="100" />
<item android:drawable="@drawable/enlarge018" android:duration="100" />
<item android:drawable="@drawable/enlarge020" android:duration="100" />
<item android:drawable="@drawable/enlarge021" android:duration="100" />
<item android:drawable="@drawable/enlarge022" android:duration="100" />
</animation-list>
'無法分配一個4.940.652字節分配與2.168.368空閒字節和2MB,直到OOM'您加載了多少個文件? – AxelH
這條線可能只是冰山一角。高內存消耗可能在代碼的其他部分。 – raxelsson
您可以使用Android Studio中的內存監視器來跟蹤您的分配情況。也許你有一些巨大的對象,由於內存泄漏而無法收集垃圾。最常見的泄露對象是上下文和位圖。您也可以嘗試一下庫LeakCanary,它會警告您內存泄漏。 –