Android Studio: java.lang.OutOfMemoryError

Derek DeLizo picture Derek DeLizo · Jun 30, 2016 · Viewed 14.4k times · Source

I am trying to get my application to display different views when Image buttons are clicked. Its a fairly simple application, but I cant test to see if it works because I keep getting an Out of Memory Error. Here is the java class

Edit:

I decreased the size of the button images by 50% and I still get an out of memory error. Additionally I added android:largeHeap="true" to the android manifest. Below are the original files but with updates

Dashboard.java

    package com.pixel.deloitte.xprizeapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TextView;

import java.io.IOException;

public class Dashboard extends AppCompatActivity {
    //private TabHost tabHost;
    //private int[] tabs = {R.drawable.dashboardnotclicked,R.drawable.teamreviewnotclicked,R.drawable.teambiosnotclicked,R.drawable.mentornotclicked,R.drawable.schedulenotclicked};
    public static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            if (ex.getClass().equals(OutOfMemoryError.class)) {
                try {
                    android.os.Debug.dumpHprofData("/sdcard/dump.hprof");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ex.printStackTrace();
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Thread.currentThread().setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dashboard);
        findViewById(R.id.login).setVisibility(View.VISIBLE);
    }

    public void buttonClick(View v){
        switch(v.getId()){
            case R.id.loginButton:
                break;
            case R.id.dashboardbtn:
                break;
            case R.id.teamreviewbtn:
                break;
            case R.id.teambios:
                break;
            case R.id.mentorsbtn:
                break;
            case R.id.schedulebtn:
                break;
        }
    }

}

Here is the error I get, the error is thrown at setContentView(R.layout.activity_dashboard)

java.lang.OutOfMemoryError

E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.pixel.deloitte.xprizeapplication, PID: 2573
                                                 java.lang.OutOfMemoryError
                                                     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
                                                     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
                                                     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                                                     at android.content.res.Resources.loadDrawable(Resources.java:2115)
                                                     at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                     at android.widget.ImageView.<init>(ImageView.java:129)
                                                     at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                     at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                     at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                     at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008)
                                                     at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067)
                                                     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                     at com.pixel.deloitte.xprizeapplication.Dashboard.onCreate(Dashboard.java:36)
                                                     at android.app.Activity.performCreate(Activity.java:5231)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                     at dalvik.system.NativeStart.main(Native Method)

Here is activity_dashboard.xml

 <?xml version="1.0" encoding="utf-8"?>
<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="900dp"
    tools:context=".Dashboard"
    android:background="@color/AppBackground">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="850dp"
        android:id="@+id/scrollView" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/login">

            <ImageView
                android:layout_width="761dp"
                android:layout_height="876dp"
                android:id="@+id/visioneervideo"
                android:src="@drawable/video" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/signin">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="295dp"
                    android:layout_height="200dp"
                    android:layout_marginBottom="203dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true">

                    <EditText
                        android:layout_width="295dp"
                        android:layout_height="49dp"
                        android:ems="10"
                        android:id="@+id/editText"
                        android:background="@drawable/username"
                        android:editable="true"
                        android:textColor="#FFFFFFFF"
                        android:textAlignment="center"
                        android:text="Username"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true" />

                    <EditText
                        android:layout_width="295dp"
                        android:layout_height="49dp"
                        android:ems="10"
                        android:id="@+id/editText2"
                        android:background="@drawable/username"
                        android:editable="true"
                        android:textColor="#FFFFFFFF"
                        android:textAlignment="center"
                        android:text="Password"
                        android:layout_below="@+id/editText"
                        android:layout_alignLeft="@+id/editText"
                        android:layout_alignStart="@+id/editText" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/loginButton"
                        android:src="@drawable/login"
                        android:onClick="buttonClick" />
                </LinearLayout>
            </RelativeLayout>

        </LinearLayout>
    </ScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="850dp"
        android:id="@+id/scrollView2"
        android:background="@color/AppBackground">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/appactivities">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:id="@+id/optionsmenu"
                android:background="@color/DashboardBackgroundnew">

                <ImageView
                    android:layout_width="241dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/xprizelogo"
                    android:background="@drawable/xprizelogo" />

                <ImageView
                    android:layout_width="141dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/dashboardbtn"
                    android:background="@drawable/ic_dashboard" />

                <ImageView
                    android:layout_width="148dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/teamreviewbtn"
                    android:background="@drawable/ic_teamreview" />

                <ImageView
                    android:layout_width="126dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/teambiosbtn"
                    android:background="@drawable/ic_teambios" />

                <ImageView
                    android:layout_width="119.5dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/mentorsbtn"
                    android:background="@drawable/ic_mentor" />

                <ImageView
                    android:layout_width="127.5dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/schedulebtn"
                    android:background="@drawable/ic_schedule" />

                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:id="@+id/twitterbtn"
                    android:background="@drawable/ic_twitter" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/profilebtn" />
            </LinearLayout>

            <ViewStub android:id="@+id/stub"
                android:inflatedId="@+id/subTree"
                android:layout_width="120dip"
                android:layout_height="40dip" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

and here is the manifest

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pixel.deloitte.xprizeapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="XPrize"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Dashboard">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Answer

itzmebibin picture itzmebibin · Jun 30, 2016

Set largeHeap property as True in AndroidManifest.xml by,

android:largeHeap="true"

So you can modify the AndroidManifest.xml as,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pixel.deloitte.xprizeapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="XPrize"
        android:supportsRtl="true"

        android:largeHeap="true"

        android:theme="@style/AppTheme">
        <activity android:name=".Dashboard">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>