4

我观看了Google IO 2011会议,阅读了几乎所有关于 OutOfMemory Exception 和 InflateException 的帖子,不走运,我找不到任何解决我问题的答案。

如何从包含背景图像的布局中正确清除内存?我觉得 InflateException 后跟 OutOfMemory 是否相关,因为该背景图像未正确清除。

所以我得到:

Android.Views.InflateException: Binary XML file line #24: Error inflating class

其次是 :

Java.Lang.OutOfMemoryError:

我很确定这是由我的背景图片引起的。

我简化了代码以尽可能缩小问题范围。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="12" />
  <activity />
  <application android:theme="@android:style/Theme.NoTitleBar" parent="android:Theme"/>
</manifest>

我添加了parent="android:Theme"应该解决问题的方法,但没有成功。

闪屏.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/SplashScreenView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splash" />

splash 是位于我的可绘制文件夹中的 .png 图像。

SplashScreenActivity.cs

[Activity(Label = "My splash screen", MainLauncher = true)]
public class SplashScreenActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.SplashScreen);
        StartNextActivity();
    }

    private void StartNextActivity()
    {
        var intent = new Intent(this, typeof(SplashScreenActivity));
        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
        StartActivity(intent);
        Finish();
    }

    public override void Finish()
    {
        base.Finish();
        var view = FindViewById<LinearLayout>(Resource.Id.SplashScreenView);

        view.Background.SetCallback(null);
        view.Background.Dispose();
        view.DestroyDrawingCache();
        Resources.FlushLayoutCache();
    }
}

首先我会SplashScreenActivity连续加载,这是为了让错误更快发生。

如您所见,我尝试了几种方法来清除背景图像、设置标志或明确尝试处置它。

在第一次加载时,我收到了这些消息,这似乎没问题,因为它正在加载图像:

07-18 12:14:22.472 I/dalvikvm-heap(30085): Grow heap (frag case) to 10.600MB for 2949136-byte allocation 
07-18 12:14:22.522 D/dalvikvm(30085): GC_CONCURRENT freed 76K, 7% free 9432K/10119K, paused 2ms+1ms

但是,它一直在增长,直到我消耗了所有设备可用内存:

07-18 12:12:17.852 I/dalvikvm-heap(18665): Clamp target GC heap from 68.605MB to 64.000MB
07-18 12:12:17.852 D/dalvikvm(18665): GC_FOR_ALLOC freed 73K, 1% free 65064K/65543K, paused 338ms
07-18 12:12:17.852 I/dalvikvm-heap(18665): Forcing collection of SoftReferences for 1576-byte allocation
07-18 12:12:18.212 I/dalvikvm-heap(18665): Clamp target GC heap from 68.604MB to 64.000MB
07-18 12:12:18.212 D/dalvikvm(18665): GC_BEFORE_OOM freed <1K, 1% free 65064K/65543K, paused 362ms
07-18 12:12:18.212 E/dalvikvm-heap(18665): Out of memory on a 1576-byte allocation.
07-18 12:12:18.212 I/dalvikvm(18665): "main" prio=5 tid=1 RUNNABLE
07-18 12:12:18.212 I/dalvikvm(18665):   | group="main" sCount=0 dsCount=0 obj=0x40a45460 self=0xb068a0
07-18 12:12:18.212 I/dalvikvm(18665):   | sysTid=18665 nice=0 sched=0/0 cgrp=default handle=1074517128
07-18 12:12:18.212 I/dalvikvm(18665):   | schedstat=( 71462466000 19081532000 88403 ) utm=6360 stm=786 core=0
07-18 12:12:18.212 I/dalvikvm(18665):   at android.content.res.Resources.getCachedStyledAttributes(Resources.java:~2193)
07-18 12:12:18.212 I/dalvikvm(18665):   at android.content.res.Resources.access$000(Resources.java:71)
07-18 12:12:18.212 I/dalvikvm(18665):   at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1260)
07-18 12:12:18.212 I/dalvikvm(18665):   at android.content.Context.obtainStyledAttributes(Context.java:364)
07-18 12:12:18.212 I/dalvikvm(18665):   at android.view.View.<init>(View.java:2746)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.ViewGroup.<init>(ViewGroup.java:385)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.widget.LinearLayout.<init>(LinearLayout.java:174)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.widget.LinearLayout.<init>(LinearLayout.java:170)
07-18 12:12:18.222 I/dalvikvm(18665):   at java.lang.reflect.Constructor.constructNative(Native Method)
07-18 12:12:18.222 I/dalvikvm(18665):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.createView(LayoutInflater.java:586)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2707)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2767)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:247)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.Activity.setContentView(Activity.java:1835)
07-18 12:12:18.222 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.n_onCreate(Native Method)
07-18 12:12:18.222 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:30)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.Activity.performCreate(Activity.java:4465)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.os.Looper.loop(Looper.java:137)
07-18 12:12:18.222 I/dalvikvm(18665):   at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 12:12:18.222 I/dalvikvm(18665):   at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:12:18.222 I/dalvikvm(18665):   at java.lang.reflect.Method.invoke(Method.java:511)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 12:12:18.222 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 12:12:18.222 I/dalvikvm(18665):   at dalvik.system.NativeStart.main(Native Method)
07-18 12:12:18.222 I/dalvikvm(18665): 
07-18 12:12:18.972 I/dalvikvm-heap(18665): Clamp target GC heap from 68.623MB to 64.000MB
07-18 12:12:18.972 D/dalvikvm(18665): GC_FOR_ALLOC freed 6K, 1% free 65082K/65543K, paused 341ms
07-18 12:12:18.982 I/dalvikvm-heap(18665): Forcing collection of SoftReferences for 1402-byte allocation
07-18 12:12:19.332 I/dalvikvm-heap(18665): Clamp target GC heap from 68.623MB to 64.000MB
07-18 12:12:19.332 D/dalvikvm(18665): GC_BEFORE_OOM freed <1K, 1% free 65082K/65543K, paused 358ms
07-18 12:12:19.332 E/dalvikvm-heap(18665): Out of memory on a 1402-byte allocation.
07-18 12:12:19.332 I/dalvikvm(18665): "main" prio=5 tid=1 RUNNABLE
07-18 12:12:19.332 I/dalvikvm(18665):   | group="main" sCount=0 dsCount=0 obj=0x40a45460 self=0xb068a0
07-18 12:12:19.332 I/dalvikvm(18665):   | sysTid=18665 nice=0 sched=0/0 cgrp=default handle=1074517128
07-18 12:12:19.342 I/dalvikvm(18665):   | schedstat=( 72173821000 19107690000 88644 ) utm=6429 stm=788 core=0
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.StringBuffer.append(StringBuffer.java:278)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.StringWriter.write(StringWriter.java:123)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.doWrite(PrintWriter.java:623)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:601)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:579)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:660)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:722)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:691)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:31)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:329)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:305)
07-18 12:12:19.342 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.n_onCreate(Native Method)
07-18 12:12:19.342 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:30)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.Activity.performCreate(Activity.java:4465)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.os.Looper.loop(Looper.java:137)
07-18 12:12:19.342 I/dalvikvm(18665):   at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:12:19.342 I/dalvikvm(18665):   at java.lang.reflect.Method.invoke(Method.java:511)
07-18 12:12:19.342 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 12:12:19.342 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 12:12:19.342 I/dalvikvm(18665):   at dalvik.system.NativeStart.main(Native Method)
07-18 12:12:19.342 I/dalvikvm(18665): 
Unhandled Exception:

Android.Views.InflateException: Binary XML file line #24: Error inflating class <unknown>

07-18 12:12:24.902 I/dalvikvm-heap(18665): Clamp target GC heap from 68.644MB to 64.000MB
07-18 12:12:24.902 D/dalvikvm(18665): GC_FOR_ALLOC freed 5K, 1% free 65105K/65543K, paused 377ms
07-18 12:12:24.902 I/dalvikvm-heap(18665): Forcing collection of SoftReferences for 1402-byte allocation
07-18 12:12:25.242 I/dalvikvm-heap(18665): Clamp target GC heap from 68.644MB to 64.000MB
07-18 12:12:25.242 D/dalvikvm(18665): GC_BEFORE_OOM freed 0K, 1% free 65105K/65543K, paused 345ms
07-18 12:12:25.242 E/dalvikvm-heap(18665): Out of memory on a 1402-byte allocation.
07-18 12:12:25.242 I/dalvikvm(18665): "main" prio=5 tid=1 RUNNABLE
07-18 12:12:25.242 I/dalvikvm(18665):   | group="main" sCount=0 dsCount=0 obj=0x40a45460 self=0xb068a0
07-18 12:12:25.242 I/dalvikvm(18665):   | sysTid=18665 nice=0 sched=0/0 cgrp=default handle=1074517128
07-18 12:12:25.242 I/dalvikvm(18665):   | schedstat=( 72950645000 19154453000 88901 ) utm=6504 stm=791 core=0
07-18 12:12:25.242 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.lang.StringBuffer.append(StringBuffer.java:278)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.StringWriter.write(StringWriter.java:123)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.doWrite(PrintWriter.java:623)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:601)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:579)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:660)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:722)
07-18 12:12:25.242 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:691)
07-18 12:12:25.252 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:31)
07-18 12:12:25.252 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:329)
07-18 12:12:25.252 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:305)
07-18 12:12:25.252 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.n_onCreate(Native Method)
07-18 12:12:25.252 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:30)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.Activity.performCreate(Activity.java:4465)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.os.Looper.loop(Looper.java:137)
07-18 12:12:25.252 I/dalvikvm(18665):   at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 12:12:25.252 I/dalvikvm(18665):   at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:12:25.252 I/dalvikvm(18665):   at java.lang.reflect.Method.invoke(Method.java:511)
07-18 12:12:25.252 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 12:12:25.252 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 12:12:25.252 I/dalvikvm(18665):   at dalvik.system.NativeStart.main(Native Method)
07-18 12:12:25.252 I/dalvikvm(18665): 
07-18 12:12:26.042 I/dalvikvm-heap(18665): Clamp target GC heap from 68.654MB to 64.000MB
07-18 12:12:26.042 D/dalvikvm(18665): GC_FOR_ALLOC freed 4K, 1% free 65114K/65543K, paused 395ms
07-18 12:12:26.042 I/dalvikvm-heap(18665): Forcing collection of SoftReferences for 1126-byte allocation
07-18 12:12:26.392 I/dalvikvm-heap(18665): Clamp target GC heap from 68.654MB to 64.000MB
07-18 12:12:26.392 D/dalvikvm(18665): GC_BEFORE_OOM freed 0K, 1% free 65114K/65543K, paused 344ms
07-18 12:12:26.392 E/dalvikvm-heap(18665): Out of memory on a 1126-byte allocation.
07-18 12:12:26.392 I/dalvikvm(18665): "main" prio=5 tid=1 RUNNABLE
07-18 12:12:26.392 I/dalvikvm(18665):   | group="main" sCount=0 dsCount=0 obj=0x40a45460 self=0xb068a0
07-18 12:12:26.392 I/dalvikvm(18665):   | sysTid=18665 nice=0 sched=0/0 cgrp=default handle=1074517128
07-18 12:12:26.392 I/dalvikvm(18665):   | schedstat=( 73695992000 19195168000 89137 ) utm=6574 stm=795 core=0
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.StringBuffer.append(StringBuffer.java:278)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.StringWriter.write(StringWriter.java:123)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.doWrite(PrintWriter.java:623)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:601)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:579)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.write(PrintWriter.java:660)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:722)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:691)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.io.PrintWriter.append(PrintWriter.java:31)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:329)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.Throwable.printStackTrace(Throwable.java:305)
07-18 12:12:26.392 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.n_onCreate(Native Method)
07-18 12:12:26.392 I/dalvikvm(18665):   at myFirstApp.android.activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:30)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.Activity.performCreate(Activity.java:4465)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.os.Looper.loop(Looper.java:137)
07-18 12:12:26.392 I/dalvikvm(18665):   at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:12:26.392 I/dalvikvm(18665):   at java.lang.reflect.Method.invoke(Method.java:511)
07-18 12:12:26.392 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 12:12:26.392 I/dalvikvm(18665):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 12:12:26.392 I/dalvikvm(18665):   at dalvik.system.NativeStart.main(Native Method)
07-18 12:12:26.392 I/dalvikvm(18665): 
Unhandled Exception:

Java.Lang.OutOfMemoryError: 

附加信息

图像是全屏的,是这个设备的完美尺寸(我的意思是我们不想缩小它)。1024x720,24 位,不是 Alpha,它在我的磁盘上占用 487KB,但显然,当 Android 加载它时,它似乎在 10 MB 左右。

我在 Nabi2 平板电脑上运行代码,型号:NABI2-NV7A,它运行 Android 版本 4.0.4 和内核版本 3.1.10-00275-g3a4f8c1。

另请注意,在我的应用程序属性中的 Visual Studio 中,我的目标是那些 Android 版本:

应用程序属性 Android 目标

4

2 回答 2

5

好的,我找到了一种方法来做到这一点而不会引发这些错误。

我没有使用启动画面创建主题,而是Layout创建了一个主题,因为在您的情况下,它只是一个图像:

样式.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="SplashTheme" parent="@android:style/Theme.NoTitleBar">
    <item name="android:background">@drawable/kitten</item>
  </style>
</resources>

然后我创建了一个这样的活动:

using Android.App;
using Android.Content;
using Android.OS;

namespace SplashScreenSample
{
    [Activity(Label = "Splushy Splushy", MainLauncher = true, Theme = "@style/SplashTheme")]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle b)
        {
            base.OnCreate(b);
            StartNextActivity();
        }

        private void StartNextActivity()
        {
            var intent = new Intent(this, typeof(SplashActivity));
            intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearWhenTaskReset);
            StartActivity(intent);
            Finish();
        }
    }
}

我试着在我的手机上运行这个程序 10 分钟左右,没有任何崩溃,一张漂亮的小猫的 1600x1200 图像。

于 2013-07-19T11:46:31.710 回答
0

我找到了另一个有帮助的解决方案,但从来没有Cheesebaron 的解决方案那么多。

在这两种情况下,它最终都会崩溃。

  • Cheesebaron 的解决方案在 14 分钟后崩溃
  • 我的在 3 分 28 秒后崩溃。

由于我正在密集循环以将设备推向极限,因此 Cheesebaron 的解决方案肯定是可行的。

在我的情况下,我没有设置android:background="@drawable/splash",而是删除了该条目并private Bitmap在我的活动中使用了一个字段,然后我将其加载并将其传递给背景可绘制对象。

private Bitmap _bitmapRef;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);        
    var view = FindViewById<LinearLayout>(Resource.Id.SplashScreenView);

    _bitmapRef = BitmapFactory.DecodeResource(Resources, Resource.Drawable.splash, new BitmapFactory.Options
                                                                      {
                                                                          InPurgeable = true,
                                                                          InInputShareable = true
                                                                      });
    view.SetBackgroundDrawable(new BitmapDrawable(Resources, _bitmapRef));
    StartNextActivity();
}

然后,在OnDestroy通话中,我重写它以显式回收位图。

protected override void OnDestroy()
{
   base.OnDestroy();

   if (null != _bitmapRef && !_bitmapRef.IsRecycled)
   {
        _bitmapRef.Recycle();
        _bitmapRef = null;
    }
}

在我看来,当在 Activity 级别创建 Bitmap 时,Android 是否有问题。

我有另一个活动,我在列表对象中加载位图以在屏幕上显示它们。列表对象处于活动级别,位图位于列表内。

在这种情况下,当我使用上述解决方案时,内存似乎得到了正确处理。我什至不必在列表内的位图上调用回收。

但对于 SplashScreen,这种解决方案似乎还不够。

于 2013-07-19T15:00:07.293 回答