0

As the title stated, my problem is related to the error. I have set 3 layout that uses the same background but there's only 1 layout that keeps producing this error. The error started when I set the background. Without the background, it can run with no problem. But the one that I want to solve is how to fix the out of memory error and why does this layout produce the error, while the other two doesn't. The code is pretty much the same.

This is the layout that produces the error.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/seabg">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:srcCompat="@drawable/f1"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="CLICK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="177dp"
    android:id="@+id/btnf"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:srcCompat="@drawable/ferry"
    android:id="@+id/imageView1"
    android:layout_marginBottom="36dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

Meanwhile, this is the one that can be run normally without the error. (It should be two layouts, saving some space because the code looks exactly the same only the first ImageView is different.)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/seabg">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:srcCompat="@drawable/y1"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="CLICK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="177dp"
    android:id="@+id/btny"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:srcCompat="@drawable/yacht"
    android:id="@+id/imageView1"
    android:layout_marginBottom="36dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

Edit: This is my activity for the one that produce the error.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_f);

    final MediaPlayer fSound = MediaPlayer.create(this, R.raw.ferry);
    btnf = (Button) findViewById(R.id.btnf);
    btnf.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            fSound.start();
        }
    });

}

And this is the one that can be run with no problem.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_y);

    final MediaPlayer ySound = MediaPlayer.create(this, R.raw.yacht);
    btny = (Button) findViewById(R.id.btny);
    btny.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            ySound.start();
        }
    });

}
HeatKai C
  • 65
  • 1
  • 9
  • 1
    Do you use the `ImageView` in your activity ? Could you post the code of the related activity ? Is it possible to include the two images as well ? – Nicolas Dusart Dec 14 '17 at 08:26
  • Agreed with Nicolas, I've encountered several OutOfMemoryError with very large images. It's very hard to track because it does not point to the image itself. – Tenten Ponce Dec 14 '17 at 08:32
  • I also encountered this issue. I recommend the documentation for a better understanding. https://developer.android.com/topic/performance/graphics/load-bitmap.html – shalama Dec 14 '17 at 08:34
  • Maybe your image is too large. You can check this https://stackoverflow.com/a/13120950/4344623 – Jeser Caneo Dec 14 '17 at 08:34
  • Edited and shown the activity codes. and now that you mentioned about the size. I think.. it maybe that is the cause. I'm gonna resize some for now. and will be updating it here again. – HeatKai C Dec 14 '17 at 08:37
  • Has been confirmed, the problem is that the image in drawable is large(in resolution size). I resize all of the one that related to the activity and everything is okay. Thanks for the fast responses, everyone here. I'm so sorry that it is such a silly mistake by me. Can one of you answer the question so I can tick it as the answer – HeatKai C Dec 14 '17 at 08:48

1 Answers1

0

Try removing the imageView or change the image src to a lower resolution! this might solve your issue!

Racer
  • 152
  • 1
  • 9
  • Thanks for the answer! It is indeed related to resolution but I don't need to remove the imageview. Thanks for your time! – HeatKai C Dec 14 '17 at 08:49