Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Finishing the User Interface Formatting Strings

Jonas Samulionis
Jonas Samulionis
6,874 Points

ERROR : java.lang.OutOfMemoryError:

Hi Guys,

Mainly I'm trying to build this app for android SDK version 23.

So in this stage I'm "getting java.lang.OutOfMemoryError:" I believe it haves someting to do with getDrawable call.

I googled it and people explains that in this version we have to use it in a different way. I'm adding my StoryAcitvity.java and the error below.

I really hope someone can help me on this stage.

private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private String mName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    Intent intent = getIntent();
    mName = intent.getStringExtra(getString(R.string.key_name));
    if (mName == null) {
        mName = "friend";
    }

    mImageView = (ImageView) findViewById(R.id.storyImageView2);
    mTextView = (TextView) findViewById(R.id.storyTextView);
    mChoice1 = (Button) findViewById(R.id.choiceButton1);
    mChoice2 = (Button) findViewById(R.id.choiceButton2);

    loadPage();

}

private void loadPage() {
    Page page = mStory.getPage(0);

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), page.getImageId(), null);
    mImageView.setImageDrawable(drawable);
    // Add the name if placeholder includes it
    String pageText = page.getText();
    pageText = String.format(pageText, mName);

    mTextView.setText(pageText);
    mChoice1.setText(page.getChoice1().getText());
    mChoice2.setText(page.getChoice2().getText());
}

}

ERROR:

java.lang.OutOfMemoryError: Failed to allocate a 26827212 byte allocation with 4194304 free bytes and 16MB 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:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635) at android.content.res.Resources.loadDrawable(Resources.java:2540) at android.content.res.TypedArray.getDrawable(TypedArray.java:870) at android.widget.ImageView.<init>(ImageView.java:152) at android.widget.ImageView.<init>(ImageView.java:140) at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:58) at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:54) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95) at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) at ge3k.lt.story.ui.StoryActivity.onCreate(StoryActivity.java:29) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 Answer

Jonas Samulionis
Jonas Samulionis
6,874 Points

Your preferred solution is not compatible with SDK 23..

It works with: Drawable drawable = ContextCompat.getDrawable(this, page.getImageId());

sachin gupta
sachin gupta
2,553 Points

I also encountered the same problem while working on the app even at lower SDK , but this solution didn't work. Please can you provide a more viable solution.