抛出OutOfMemoryError"无法分配带有15229320个空闲字节的18485292字节分配和14MB直到OOM"

时间:2016-04-12 15:51:26

标签: java android

我在一个活动中出现了这个错误:内存不足:

  

流程:com.moments.hamza.babytalk,PID:16463                                                                               java.lang.OutOfMemoryError:无法分配18485292字节分配,其中15230424个空闲字节和14MB直到OOM                                                                                   at dalvik.system.VMRuntime.newNonMovableArray(Native Method)                                                                                   在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                                   在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:747)                                                                                   在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:568)                                                                                   在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)                                                                                   在android.content.res.Resources.loadDrawableForCookie(Resources.java:3747)                                                                                   在android.content.res.Resources.loadDrawable(Resources.java:3620)                                                                                   在android.content.res.TypedArray.getDrawable(TypedArray.java:762)                                                                                   在android.widget.ImageView。(ImageView.java:151)                                                                                   在android.widget.ImageView。(ImageView.java:140)                                                                                   在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:57)                                                                                   在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:53)                                                                                   在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)                                                                                   在android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)                                                                                   在android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)                                                                                   在android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)                                                                                   在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)                                                                                   在android.view.LayoutInflater.rInflate(LayoutInflater.java:813)                                                                                   在android.view.LayoutInflater.rInflate(LayoutInflater.java:821)                                                                                   在android.view.LayoutInflater.inflate(LayoutInflater.java:511)                                                                                   在android.view.LayoutInflater.inflate(LayoutInflater.java:415)                                                                                   在android.view.LayoutInflater.inflate(LayoutInflater.java:366)                                                                                   在android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)                                                                                   在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)                                                                                   在com.moments.hamza.babytalk.Level_2.onCreate(Level_2.java:49)                                                                                   在android.app.Activity.performCreate(Activity.java:6283)                                                                                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)                                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)                                                                                   在android.app.ActivityThread.access $ 900(ActivityThread.java:177)                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:145)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5942)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.moments.hamza.babytalk.Level_2"
    android:padding="16dp">


    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/v_f_level2"
        android:layout_marginBottom="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_aid"
            android:src="@drawable/letter_a"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_bid"
            android:src="@drawable/letter_b"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_cid"
            android:src="@drawable/letter_c"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_did"
            android:src="@drawable/letter_d"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_eid"
            android:src="@drawable/letter_e"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_fid"
            android:src="@drawable/letter_f"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_gid"
            android:src="@drawable/letter_g"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_hid"
            android:src="@drawable/letter_h"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_iid"
            android:src="@drawable/letter_i"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_jid"
            android:src="@drawable/letter_j"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_kid"
            android:src="@drawable/letter_k"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_lid"
            android:src="@drawable/letter_l"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_mid"
            android:src="@drawable/letter_m"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_nid"
            android:src="@drawable/letter_n"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_oid"
            android:src="@drawable/letter_o"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_pid"
            android:src="@drawable/letter_p"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_qid"
            android:src="@drawable/letter_q"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_rid"
            android:src="@drawable/letter_r"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_sid"
            android:src="@drawable/letter_s"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_tid"
            android:src="@drawable/letter_t"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_uid"
            android:src="@drawable/letter_u"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_vid"
            android:src="@drawable/letter_v"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_wid"
            android:src="@drawable/letter_w"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_xid"
            android:src="@drawable/letter_x"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_yid"
            android:src="@drawable/letter_y"
            android:scaleType="centerCrop" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/letter_zid"
            android:src="@drawable/letter_z"
            android:scaleType="centerCrop"
            android:contentDescription="@string/a_letter" />

    </ViewFlipper>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/next_level_2"
        android:src="@drawable/next"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/back_level_2"
        android:src="@drawable/back"
        android:layout_alignTop="@+id/next_level_2"
        android:layout_alignParentStart="true" />

</RelativeLayout>

level_2的主要代码是:

public class Level_2 extends AppCompatActivity {
private ImageView back_level_2;
private ImageView next_level_2;

private ImageView letteraj;
private ImageView letterbj;
private ImageView lettercj;
private ImageView letterdj;
private ImageView letterej;
private ImageView letterfj;
private ImageView lettergj;
private ImageView letterhj;
private ImageView letterij;
private ImageView letterjj;
private ImageView letterkj;
private ImageView letterlj;
private ImageView lettermj;
private ImageView letternj;
private ImageView letteroj;
private ImageView letterpj;
private ImageView letterqj;
private ImageView letterrj;
private ImageView lettersj;
private ImageView lettertj;
private ImageView letteruj;
private ImageView lettervj;
private ImageView letterwj;
private ImageView letterxj;
private ImageView letteryj;
private ImageView letterzj;

private ViewFlipper v_f_level2j;
private TextToSpeech t1;

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

    v_f_level2j = (ViewFlipper) findViewById(R.id.v_f_level2);

    back_level_2 = (ImageView) findViewById(R.id.next_level_2);
    next_level_2 = (ImageView) findViewById(R.id.back_level_2);

    letteraj=(ImageView) findViewById(R.id.letter_aid);
    letterbj=(ImageView) findViewById(R.id.letter_bid);
    lettercj=(ImageView) findViewById(R.id.letter_cid);
    letterdj=(ImageView) findViewById(R.id.letter_did);
    letterej=(ImageView) findViewById(R.id.letter_eid);
    letterfj=(ImageView) findViewById(R.id.letter_fid);
    lettergj=(ImageView) findViewById(R.id.letter_gid);
    letterhj=(ImageView) findViewById(R.id.letter_hid);
    letterij=(ImageView) findViewById(R.id.letter_iid);
    letterjj=(ImageView) findViewById(R.id.letter_jid);
    letterkj=(ImageView) findViewById(R.id.letter_kid);
    letterlj=(ImageView) findViewById(R.id.letter_lid);
    lettermj=(ImageView) findViewById(R.id.letter_mid);
    letternj=(ImageView) findViewById(R.id.letter_nid);
    letteroj=(ImageView) findViewById(R.id.letter_oid);
    letterpj=(ImageView) findViewById(R.id.letter_pid);
    letterqj=(ImageView) findViewById(R.id.letter_qid);
    letterrj=(ImageView) findViewById(R.id.letter_rid);
    lettersj=(ImageView) findViewById(R.id.letter_sid);
    lettertj=(ImageView) findViewById(R.id.letter_tid);
    letteruj=(ImageView) findViewById(R.id.letter_uid);
    lettervj=(ImageView) findViewById(R.id.letter_vid);
    letterwj=(ImageView) findViewById(R.id.letter_wid);
    letterxj=(ImageView) findViewById(R.id.letter_xid);
    letteryj=(ImageView) findViewById(R.id.letter_yid);
    letterzj=(ImageView) findViewById(R.id.letter_zid);

    t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                t1.setLanguage(Locale.UK);

            }
        }
    });
    letteraj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="A";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterbj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="B";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettercj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="C";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterdj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="D";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterej.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="E";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterfj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="F";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettergj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="G";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterhj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="H";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterij.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="I";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterjj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="J";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterkj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="K";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterlj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="L";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettermj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="M";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letternj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="N";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letteroj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="O";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterpj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="P";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterqj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="Q";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterrj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="R";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettersj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="S";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettertj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="T";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letteruj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="U";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    lettervj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="V";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterwj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="W";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak, TextToSpeech.QUEUE_ADD, null);}});
    letterxj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="X";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letteryj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="Y";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
    letterzj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak ="Z";
            t1.setSpeechRate((float) 1);
            t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});





    next_level_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v_f_level2j.showNext();
        }

    });


    back_level_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v_f_level2j.showPrevious();
        }

    });

}
}
你能帮帮我吗?

1 个答案:

答案 0 :(得分:6)

您的一个或多个ImageView小部件指向的资源大约为2150 x 2150像素。这太大了。使用较小的图像。

  

最大尺寸为:28KB

磁盘上的大小,这没什么用处。

  

最大尺寸为660X778

然后我的猜测是您将图像放在res/drawable/中,并且您正在使用-xxhdpi密度的设备或模拟器进行测试。 res/drawable/res/drawable-mdpi/的同义词,其中的drawables适用于中密度屏幕。当您在-xxhdpi密度环境中加载它们时,这些图像将在每个轴上按比例缩放3倍,以使图像保持与-mdpi设备上相同的物理尺寸。 / p>