4

When I create a RelativeLayout(or a Button, textview etc) in Android programmatically:

val rl: RelativeLayout = RelativeLayout (this)

Now, can there be a scenario when the OS is not able to allocate any memory to create the RelativeLayout? If yes then How to handle it?

In the documentation, it is not mentioned anywhere if it will return null in this scenario (which I think it won't as rl is non-nullable and the above code doesn't give any compilation error) or if it will give an OutOfMemoryError exception and I have to use a try-catch to handle it.

Is this behavior documented anywhere?

1 Answers1

-1

Yes, it may occur. try using ErrorCatch Exception.

    try {
        val rl: RelativeLayout = RelativeLayout(this)

    } catch (e: OutOfMemoryError) {
        println(e.toString)
    }
xoxoxo
  • 1
  • 3
  • 1
    Is this behavior documented anywhere? that It will give an OutOfMemoryError exception error – Jatin guglani Aug 29 '23 at 09:51
  • You [need to be very careful](https://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror) about catching `OutOfMemoryError`. – Jorn Aug 29 '23 at 12:02