由于可绘制对象的 OutOfMemoryException,应用程序崩溃

App crashing due to OutOfMemoryException from drawables

当我点击 NormalMode class 上的 "level" Button (btnlvlform) 时,它显示 UNFORTUNATELY, (MY GAME) has stopped 我可以'不知道为什么。我看不到错误。我已经检查了 LevelForm Class 和 NormalMode 以及它们对应的 XML。请帮助我。

正常模式class:

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);

    }
  }
}

当我点击 btnlvlform 时,它会转到这里。

LvlForm1.java(水平形式)

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;

   }

}

这是 XML NormalMode.java

<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>

Logcat

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.access0(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

您的一个背景可绘制对象(背景图像)太大,因此您的应用内存不足

检查您的图像并缩放它们。 @drawable/backgroundtemporary @drawable/levelform @drawable/back2

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