使用 PNG 图像的 SplashScreen 导致 Android.Views.InflateException 然后是 OutOfMemory

Posted

技术标签:

【中文标题】使用 PNG 图像的 SplashScreen 导致 Android.Views.InflateException 然后是 OutOfMemory【英文标题】:SplashScreen using PNG image leads to Android.Views.InflateException followed by OutOfMemory 【发布时间】:2013-07-17 18:15:34 【问题描述】:

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

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

所以我得到了:

android.Views.InflateException:二进制 XML 文件第 24 行:错误 膨胀类

后跟:

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",这应该可以解决问题,但没有成功。

SplashScreen.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_
    android:layout_
    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 版本:

【问题讨论】:

启动画面有多大?也许值得将图像修剪为仅内容,将其居中对齐并更改视图的背景颜色? @jimmithy 信息在最后一节中更新,称为附加信息。 你这里没有循环吗? StartNextActivity 启动 SplashScreenActivity 并且这将不断发生,直到您耗尽内存... @Cheesebaron 是的,这就是我提到的,我使用循环来重现在我的应用程序的正常过程中需要更多时间才能发生的问题。但是,在这两种情况下,当上一个活动完成时,背景图像数据似乎都没有被清除。 【参考方案1】:

好的,我找到了一种不抛出这些错误的方法。

我没有使用初始屏幕创建Layout,而是创建了一个主题,因为在您的情况下,它只是一个图像:

styles.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 的漂亮小猫的图像。

【讨论】:

非常感谢,您的解决方案比我找到的解决方案更可行。除了我的 SplashScreen 之外,我还发布了一个运行良好的解决方案。我很想知道你是怎么想的。使用您的解决方案(结合我的其他活动),我相信在正常使用我的应用程序时,不会出现内存问题。【参考方案2】:

我找到了另一个有用的解决方案,但从来没有像 Cheesebaron's solution 那么多。

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

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 级别创建位图时,Android 是否有问题。

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

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

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

【讨论】:

其实你可以在将Bitmap分配给ImageView之后立即处理它,图像仍然存在于Java端,并且最终会被处理掉不再显示。因此,您可以简单地将您获取的 Bitmap 包含在 using 语句中。 @Cheesebaron 不幸的是,当我这样做时,图像永远不会显示。如果我没有在using(...) 子句中调用bitmapRef.recycle,但随后我收到了奇怪的消息mmap failed for imageref_ashmem size=2949120 err=12,然后是mmap(0,579525) failed: Out of memory and crashes with Assertion at /Users/builder/data/lanes/monodroid-mlion-master/294d4619/source/mono/mono/mini/‌​mini.c:3986, condition code not met. Got a SIGSEGV while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application.

以上是关于使用 PNG 图像的 SplashScreen 导致 Android.Views.InflateException 然后是 OutOfMemory的主要内容,如果未能解决你的问题,请参考以下文章

Ionic cordova-plugin-splashscreen

在 Win 10 Mobile 上返回屏幕尺寸的 Splashscreen Rect 坐标

PhoneGap Build Android 不显示闪屏

java基础思维导图

无法在我的 iPhone 应用程序中为横向设置 SplashScreen

docker核心技术导图分享