2016-04-12 38 views
1

我有這個錯誤在一個活動回合的內存不足:拋出的OutOfMemoryError「無法分配與15229320個免費字節18485292字節分配和14MB直到OOM」

Process: com.moments.hamza.babytalk, PID: 16463 java.lang.OutOfMemoryError: Failed to allocate a 18485292 byte allocation with 15230424 free bytes and 14MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:747) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:568) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014) at android.content.res.Resources.loadDrawableForCookie(Resources.java:3747) at android.content.res.Resources.loadDrawable(Resources.java:3620) at android.content.res.TypedArray.getDrawable(TypedArray.java:762) at android.widget.ImageView.(ImageView.java:151) at android.widget.ImageView.(ImageView.java:140) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:57) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:53) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102) at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at android.view.LayoutInflater.inflate(LayoutInflater.java:366) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) at com.moments.hamza.babytalk.Level_2.onCreate(Level_2.java:49) at android.app.Activity.performCreate(Activity.java:6283) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at 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(); 
     } 

    }); 

} 
} 

你能幫幫我嗎?

+0

很可能你有一張使用太多內存的圖像。注意:圖像在內存中未壓縮,因此它們比壓縮的尺寸大得多。 –

+0

請發佈整個堆棧跟蹤。 – CommonsWare

+0

@CommonsWare我不明白(整個堆棧跟蹤) –

回答

5

您的一個或多個ImageView小部件指向的資源大約爲2150 x 2150像素。這太大了。使用較小的圖像。

the largest size is : 28KB

即在磁盤上,這是沒有多大用處的大小

the largest dimension is 660X778

然後我的猜測是,你把圖像res/drawable/,和你用-xxhdpi密度的設備或模擬器上測試。 res/drawable/res/drawable-mdpi/的同義詞,並且這些可繪製的圖形用於中密度屏幕。當您在密度環境中加載它們時,這些圖像將在每個軸上按比例縮放3倍,以使圖像保持與在-mdpi設備上相同的物理尺寸。

+0

只是爲了好奇,從哪裏抓到了這個? –

+0

最大尺寸爲:28KB,最大尺寸爲660X778 –

+4

@Prera​​kSola:堆棧跟蹤顯示錯誤來自獲取「ImageView」的可繪製資源。圖像佔用18485292字節/ 4字節/像素= 4621323像素。如果這幅圖像是正方形的,那麼這將是〜2150。當然,圖像可能不是方形的。 – CommonsWare

相關問題