I have been trying to load a very basic activity with on ImageButton, 2 textViews, a textField and an ImageView.
<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:clickable="false"
android:id="@+id/namescreenactivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome to your Pet House"
android:id="@+id/Welcome"
android:textColor="#ff000000"
android:textStyle="bold"
android:layout_marginTop="94dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_marginTop="36dp"
android:textColor="#ff000000"
android:layout_below="@+id/Welcome"
android:layout_alignEnd="@+id/Welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name your pet"
android:id="@+id/NameLabel"
android:layout_alignTop="@+id/edit_message"
android:layout_alignStart="@+id/Welcome" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/sun"
android:layout_alignBottom="@+id/Welcome"
android:layout_alignParentEnd="true"
android:layout_marginBottom="39dp"
android:id="@+id/sun" />
<ImageButton
android:layout_width="250dp"
android:layout_height="250dp"
android:id="@+id/imageButton"
android:src="@drawable/camel"
android:clickable="true"
android:scaleType="fitXY"
android:onClick="sendMessage"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/edit_message"
android:layout_marginBottom="31dp" />
And in MainActivity.java's OnCreate function I am simply calling setContentView(R.layout.activity_main); However, my application's memory usage graph shows that I am utilizing around 44MB (Although the total size of all my images in the app is less than 1MB and the one used for the ImageButton is 193KB).
I believe that this is the reason my app crashes and throws an Out of Memory error
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138018828 byte allocation with 4194304 free bytes and 35MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
at android.content.res.Resources.loadDrawable(Resources.java:2381)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageButton.<init>(ImageButton.java:86)
at android.widget.ImageButton.<init>(ImageButton.java:82)
at android.widget.ImageButton.<init>(ImageButton.java:78)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.healthypetdhr.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)'
Please help me figure out what's wrong and how I can resolve this issue.