应用停止工作

时间:2017-08-18 10:58:52

标签: java android out-of-memory splash-screen

我无法弄清楚我做错了什么。 我是一个完整的编码初学者,所以不要生气:)

基本上我想在我的闪屏中做一个动画背景。 但每次启动它时,应用程序都会停止工作。

下面是代码和错误:

欢迎活动(启动):

public class WelcomeActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 4000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();



        ImageView bgAni = (ImageView) findViewById(R.id.imageBG);
        bgAni.setBackgroundResource(R.drawable.animation_list);

        AnimationDrawable pro = (AnimationDrawable) bgAni.getBackground();
        pro.start();


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent welcomeIntent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(welcomeIntent);
                finish();
            }
        }, SPLASH_TIME_OUT);

    }
}

主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

欢迎使用XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="de.procre.togetheraware.WelcomeActivity">

    <ImageView
        android:id="@+id/imageBG"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/animation_list"
        android:scaleType="fitXY"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@color/pure_white"/>


</RelativeLayout>

动画列表:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/anim_background" android:oneshot="true">
    <item android:drawable="@drawable/frame0000" android:duration="30" />
    <item android:drawable="@drawable/frame0001" android:duration="30" />
.
.
.
    <item android:drawable="@drawable/frame0124" android:duration="30" />

</animation-list>

错误代码:

08-18 12:45:41.171 13517-13517/de.procre.togetheraware E/AndroidRuntime: FATAL EXCEPTION: main                                                                        
         Process: de.procre.togetheraware, PID: 13517
         java.lang.OutOfMemoryError: Failed to allocate a 6350412 byte allocation with 4120384 free bytes and 3MB until OOM
         at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
         at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
         at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
         at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
         at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
         at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
         at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
         at android.content.res.Resources.loadDrawable(Resources.java:854)
         at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
         at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:327)
         at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:297)
         at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
         at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1227)
         at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1200)
         at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:715)
         at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
         at android.content.res.Resources.loadDrawable(Resources.java:854)
         at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
         at android.widget.ImageView.<init>(ImageView.java:157)
         at android.widget.ImageView.<init>(ImageView.java:145)
         at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
         at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
         at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
         at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
         at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
         at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
         at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
         at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
         at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
         at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
         at de.procre.togetheraware.WelcomeActivity.onCreate(WelcomeActivity.java:22)
         at android.app.Activity.performCreate(Activity.java:6662)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
         at android.app.ActivityThread.-wrap12(ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:154)
         at android.app.ActivityThread.main(ActivityThread.java:6077)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

4 个答案:

答案 0 :(得分:2)

在这里你的内存不足,应用程序无法分配必要的内存有一个解决方案,您可以将此行放在应用程序标记清单

android:largeHeap="true"

答案 1 :(得分:2)

尝试在makeiest文件的应用程序标记中make make android:hardwareAccelerated="false"android:largeHeap="true" 像这样

<application
 android:largeHeap="true"
 android:hardwareAccelerated="false">

答案 2 :(得分:0)

在您的清单中,application下包含以下内容:

<application
    ...
    ..
    .
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:largeHeap="true"
    android:supportsRtl="true"
    .
    ..
    ...>

在调用意图后取消你使用的finish();

答案 3 :(得分:0)

<animation-list .......... </animation-list>中,您使用 124 drawables 制作动画,即使添加largeheap,这肯定会导致内存不足,这不是一个好习惯。请参考here此库以创建动画启动....或选择其他替代方案