应用程序因drawables的OutOfMemoryException而崩溃

时间:2015-01-20 20:53:36

标签: java android out-of-memory android-drawable

当我点击"等级"我Button课程btnlvlformNormalModeUNFORTUNATELY, (MY GAME) has stoppedLevelForm,我无法弄明白为什么。我无法看到错误。我已经检查过NormalMode类和public class NormalMode extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_normal_mode); } public void action(View v){ int id = v.getId(); if (id==R.id.btnlvlform){ Intent intent= new Intent(this,LvlForm1.class); startActivity(intent); } if (id==R.id.btnback2){ Intent intent= new Intent(this,StartForm.class); startActivity(intent); } } } 以及相应的XML。请帮帮我。

NORMALMODE类:

btnlvlform

当我点击public class LvlForm1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lvl_form1); } public void action(View v){ int id = v.getId(); if (id==R.id.btnluneta1){ Intent intent= new Intent(this,NewGame1.class); startActivity(intent); } if (id==R.id.btnvigan1){ Intent intent= new Intent(this,NewGame2.class); startActivity(intent); } if (id==R.id.btnleyte1){ Intent intent= new Intent(this,NewGame3.class); startActivity(intent); } if (id==R.id.btnpaoay1){ Intent intent= new Intent(this,NewGame4.class); startActivity(intent); } if (id==R.id.btnbataan1){ Intent intent= new Intent(this,NewGame5.class); startActivity(intent); } if (id==R.id.btnback5){ Intent intent= new Intent(this,NormalMode.class); startActivity(intent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.lvl_form1, menu); return true; } } 时,它会转到此处。

LvlForm1.java(LEVEL FORM)

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/backgroundtemporary"
tools:context=".NormalMode" >

<Button
    android:id="@+id/btnlvlform"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="127dp"
    android:background="@drawable/levelform" 
    android:onClick="action"/>

<Button
    android:id="@+id/btnback2"
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="25dp"
    android:layout_marginRight="23dp"
    android:background="@drawable/back2" 
    android:onClick="action"/>

</RelativeLayout>

这是NormalMode.java的XML

01-20 21:16:42.532: E/dalvikvm-heap(1102): Out of memory on a 14785968-byte allocation.
01-20 21:16:42.540: E/AndroidRuntime(1102): FATAL EXCEPTION: main
01-20 21:16:42.540: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luksongbakagame/com.example.luksongbakagame.LvlForm1}: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.os.Looper.loop(Looper.java:137)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at dalvik.system.NativeStart.main(Native Method)
01-20 21:16:42.540: E/AndroidRuntime(1102): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.Activity.setContentView(Activity.java:1881)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at com.example.luksongbakagame.LvlForm1.onCreate(LvlForm1.java:14)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.Activity.performCreate(Activity.java:5104)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-20 21:16:42.540: E/AndroidRuntime(1102):     ... 11 more
01-20 21:16:42.540: E/AndroidRuntime(1102): Caused by: java.lang.reflect.InvocationTargetException
01-20 21:16:42.540: E/AndroidRuntime(1102):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-20 21:16:42.540: E/AndroidRuntime(1102):     ... 24 more
01-20 21:16:42.540: E/AndroidRuntime(1102): Caused by: java.lang.OutOfMemoryError
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.view.View.<init>(View.java:3330)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.widget.TextView.<init>(TextView.java:583)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.widget.Button.<init>(Button.java:107)
01-20 21:16:42.540: E/AndroidRuntime(1102):     at android.widget.Button.<init>(Button.java:103)
01-20 21:16:42.540: E/AndroidRuntime(1102):     ... 27 more

logcat的

{{1}}

1 个答案:

答案 0 :(得分:0)

你的背景画面(背景图片)远远不够大,因此你的应用程序内存不足

检查图像并缩放图像。 @drawable/backgroundtemporary @drawable/levelform @drawable/back2

您应该为不同的屏幕密度提供不同大小的不同图像。