2016-03-13 85 views
0

我正在寫一個android應用程序,並且遇到問題。每隔一段時間,當切換活動時,應用程序在setContentView上崩潰。它並不總是在同一個活動上。該錯誤消息我得到的是如下:Android應用程序在切換活動時崩潰

產生的原因:java.lang.OutOfMemoryError:無法分配與16777216個免費字節一個字節24263724分配和22MB直到OOM

產生的原因:機器人.view.InflateException:二進制XML文件行#1:錯誤膨脹類

任何幫助將不勝感激。

編輯:每次打開第三個新活動時都會發生這種情況。

下面是我的一些代碼: 活動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); 
} 

活動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" /> 

+0

您在某種程度上要求系統分配近2 Gb的內存。也許一些動態數組太大,這是由一個錯誤引起的。如果你自己找不到解決方案,請顯示一些你的代碼。 –

+0

我認爲這是一個無限循環的情況。請顯示你的代碼。 – OShiffer

+0

剛剛添加了代碼 –

回答

0

可以使用Android:largeHea在您的Android清單中請求一個更大的堆大小p =「true」

+0

在這種情況下,這不是一個好主意:[「絕不會因爲內存不足而請求較大的堆大小]」(http://developer.android.com/training/articles/memory.html# CheckHowMuchMemory) – PPartisan

+0

這有效,但更好的解決方案會有所幫助。 –

+0

請發佈堆棧跟蹤並描述通過putExtra()添加的對象類型。 – hg123

相關問題