简体   繁体   中英

Android app crashes when switching activities

I am writing an android app, and have run into a problem. Every once in a while, when switching activities, the app crashes on setContentView. It is not always on the same activity. The error message I get is as follows:

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 24263724 byte allocation with 16777216 free bytes and 22MB until OOM

and

Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class

Any help would be greatly appreciated.

Edit: This happens every time a third new activity is opened.

Here is some of my code: Activity 1

public void firstroom(View view){
    Intent firstroom=new Intent(this, firstroom.class);
    putextras(firstroom);
    startActivity(firstroom);
}
public void putextras(Intent play){
    play.putExtra("doortoroom2", doortoroom2);
    play.putExtra("doortoroom3", doortoroom3);
    play.putExtra("endlockkey", endlockkey);
    play.putExtra("endlockcombo", endlockcombo);
    play.putExtra("endlockcolor", endlockcolor);
    play.putExtra("havekey", havekey);
    play.putExtra("havecrowbar", havecrowbar);
    play.putExtra("haveflashlight", haveflashlight);
    play.putExtra("keyselected", keyselected);
    play.putExtra("crowbarselected", crowbarselected);
    play.putExtra("flashlightselected", flashlightselected);
}

Activity 2

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_firstroom);

XML

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mattdevelopment01.mateusz.escaperoom.escaperoom.firstroom"
android:background="@drawable/firstroom">

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:id="@+id/secondroom"
    android:layout_marginBottom="52dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@android:color/transparent"
    android:onClick="blueroom" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:id="@+id/end"
    android:background="@android:color/transparent"
    android:layout_alignTop="@+id/secondroom"
    android:layout_toRightOf="@+id/secondroom"
    android:layout_toEndOf="@+id/secondroom"
    android:onClick="freedom" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="75dp"
    android:layout_height="35dp"
    android:id="@+id/topdrawer"
    android:layout_marginLeft="73dp"
    android:layout_marginStart="73dp"
    android:layout_marginBottom="37dp"
    android:layout_alignBottom="@+id/end"
    android:layout_toRightOf="@+id/end"
    android:layout_toEndOf="@+id/end"
    android:background="@android:color/transparent"
    android:onClick="topdrawer" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="75dp"
    android:layout_height="35dp"
    android:id="@+id/bottomdrawer"
    android:background="@android:color/transparent"
    android:layout_alignBottom="@+id/end"
    android:layout_alignLeft="@+id/topdrawer"
    android:layout_alignStart="@+id/topdrawer"
    android:onClick="bottomdrawer" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/endkey"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:background="@android:color/transparent"
    android:layout_above="@+id/endcombo"
    android:layout_alignLeft="@+id/end"
    android:layout_alignStart="@+id/end"
    android:onClick="endkey" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="50dp"
    android:layout_height="25dp"
    android:id="@+id/endcombo"
    android:background="@android:color/transparent"
    android:layout_above="@+id/endcolor"
    android:layout_alignLeft="@+id/endcolor"
    android:layout_alignStart="@+id/endcolor"
    android:onClick="endcombo" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="50dp"
    android:layout_height="25dp"
    android:id="@+id/endcolor"
    android:background="@android:color/transparent"
    android:layout_alignBottom="@+id/topdrawer"
    android:layout_alignLeft="@+id/endkey"
    android:layout_alignStart="@+id/endkey"
    android:onClick="endcolor" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="50dp"
    android:layout_height="45dp"
    android:id="@+id/key"
    android:layout_marginLeft="51dp"
    android:layout_marginStart="51dp"
    android:background="@drawable/key"
    android:onClick="key"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="50dp"
    android:layout_height="45dp"
    android:id="@+id/flashlight"
    android:background="@drawable/flashlight"
    android:onClick="flashlight"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/crowbarget"
    android:layout_toEndOf="@+id/crowbarget" />

<Button
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="50dp"
    android:layout_height="45dp"
    android:id="@+id/crowbarget"
    android:background="@drawable/crowbar"
    android:onClick="crowbar"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/key"
    android:layout_toEndOf="@+id/key" />

您可以在Android清单中使用android:largeHeap =“ true”请求更大的堆大小

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM