-1

So, heres the point:

I'm training up and making my first steps in android development and after I narrowed down a few errors myself, I finally came across one that I cant find the solution for.

I want to create an app that displays two pictures that split the screen in half horizontally. So I create a LinearLayout with a vertical orientation and add two RelativeLayout's inside it. I compile it and the build apparently completes but as soon as the app tries to start up on my mobile (Samsung S5 Neo) it says it stopped working and I land up on my desktop again. >.< Also, before tips alike arise - yes I started a new project for testing purposes already and the same error occurs. Also, when I remove that lines of code, the app works just fine on my mobile. So, that's my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="removedbecausenotyourbusiness:P">

    <RelativeLayout
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent">

        <ImageView
            android:id="@+id/background2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background2"
            android:scaleType="centerCrop"/>
    </RelativeLayout>

    <RelativeLayout
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent">

        <ImageView
            android:id="@+id/background1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background1"
            android:scaleType="centerCrop"/>
    </RelativeLayout>

</LinearLayout>

Also, I'd like to get to know if I can somehow get a log from my mobile as to WHY the app stopped working. Where can I get this?

EDIT: I believe I found what you were asking me for.

12-17 19:27:18.751 11830-11830/? E/Zygote: v2 12-17 19:27:18.751 11830-11830/? E/Zygote: accessInfo : 0 12-17 19:27:20.211 11830-11830/com.example.marco E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.marco, PID: 11830 java.lang.OutOfMemoryError: Failed to allocate a 231444012 byte allocation with 4182352 free bytes and 125MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228) at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211) at android.content.res.Resources.loadDrawable(Resources.java:4085) at android.content.res.Resources.loadDrawable(Resources.java:3935) at android.content.res.TypedArray.getDrawable(TypedArray.java:886) at android.widget.ImageView.(ImageView.java:157) at android.widget.ImageView.(ImageView.java:145) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:60) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) at android.view.LayoutInflater.inflate(LayoutInflater.java:527) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143) at com.example.marco.MainActivity.onCreate(MainActivity.java:11) at android.app.Activity.performCreate(Activity.java:6877) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7230) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Eldelshell
  • 6,683
  • 7
  • 44
  • 63
Devaron
  • 107
  • 2

1 Answers1

0

Here is some things you can do to fix the OutOfMemoryError from your images:

Add these lines in your AndroidManifest file under the application at the top:

android:hardwareAccelerated="false"

android:largeHeap="true"

So the very top of of your AndroidManifest will look something like this with the two added lines:

    <application
        android:hardwareAccelerated="false"  //added
        android:largeHeap="true"             //added
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

If that doesn't fix it, then you will need to compress your image files. You can do that on a site like https://tinyjpg.com/. It is probably just an issue of your images being too large!

Also next time you run into a problem like this, you need to use that error that you received in your crash log / stacktrace and search for the solution. In your case, you could have found this: Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

Community
  • 1
  • 1
Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
  • True.. I guess the proper way to do this would be with an image library like Picasso. – Nick Friskel Dec 17 '16 at 23:48
  • That could be one solution. Another solution can be to shrink the image in size because loading a small ic-launcher does not crash the app. So the image is probably big. Not sure what Picasso does but it could be a solution that isn't a workaround – Zoe Dec 18 '16 at 19:08