java.lang.illegalstateexception: unable to create layer for linearlayout

Found most of this crashes on Android 6.0 around 75% of the total crashes and 25% from Android 5.0. According to google Crashlytics  report, these devices areOnePlus One, OnePlus X, Xperia Z, Xperia Z1, Xperia Z2, Xperia Z3, Samsung Galaxy Note, Galaxy S4 and Galaxy S5 devices, covering 80% of total crashes. Not able to recreate this crash on my HTC, Mi devices. How to reproduce this issue or find where exact the issue in app? Below is the detailed log from Crashlytics.
Fatal Exception: java.lang.IllegalStateException: Unable to create layer for LinearLayout, size 1080x4160 exceeds max size 4096
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:323)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:6186)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Rectify and solve issue:

I have Nested scroll view which contains vertical recycler view and other elements in the main screen.

What is the exact issue :
Cause is that RecyclerView inside scrollview or nestedscrollview try to load all items at once. As RecyclerView loads all items, also those items whcih are not visible. If you put log inside onBindViewHolder of RecylerView you find that all items loads at once in the start instead of based on the visibility of the item. This causes the parent LinearLayout (inside the ScrollView) to throw exception. I had also issue related to same with Recyclerview inside NestedScrollView.

Issue cause related  to above discussion:
If UI load more than 1-1.5 screens of content at a time.
Means you are loading unncessasary content which are not visible to user at a time on mobile screen. (While vertical RecyclerView inside NestedScrollView loads all its items at once in starting, instead of based on Recyclerview Visibility)

In the below answer @ivan V found issue related to the same and also given suggestions.
https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-32966150

If you have crashlytics log for this issue then you can rectify the android Device and Android OS for which this type of Crashnalytics log detected.

Ho to detect issue:
1. Find all such activities whcih contains layout with LinearLayout.
2. Run you application and search your log and open each of your activity. Try to find during which activity log similar to below is detected.
W/OpenGLRenderer(18137): Layer exceeds max. dimensions supported by the GPU (1080x4628, max=4096x4096)
E/AndroidRuntime(18137): java.lang.IllegalStateException: Unable to create layer for  RelativeLayout
OR
Unable to create layer for v
OR
Fatal Exception: java.lang.IllegalStateException: Unable to create layer for LinearLayout, size 1080x4160 exceeds max size 4096
Reference: https://stackoverflow.com/questions/26626344/scene-transition-with-hero-elements-throws-layer-exceeds-max-dimensions-support#answer-27995911
Reference 2: https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-27906489

Reference 3: Scene transition with hero elements throws Layer exceeds max. dimensions supported by the GPU

Solution :
Paginate your content and load no more than 1-1.5 screens of content at a time.

This will significantly decrease the screen size that is being pre-rendered during shared elements transition (it's being pre-rendered every time so the system knows where to place your transition object after transition)

Best Reference for solution:  https://stackoverflow.com/questions/26664922/crash-material-design-android-5-0/27906489#answer-32966150

I had the same issue & How I solved it:

My Layout was like below mentioned:
I have refactored my code such that there is only one main recycler view and all other layout are child of it. Using the RecycleView with multiple types of child views

Here I have replaced the logic part and UI part of Nested Scroll View items to different type of Recycler View holders inside Main RecyclerView.

You can see in bottom table I have replaced the Nested Scroll View and Linear-layout with a New Main RecyclerView.


BeforeAfter Refactoring from Scroll View to Recycler View
Nested Scroll View Main layout-> RecyclerView
LinearLayout
Horizontal Recycler View Child item layout-> Horizontal Recycler View
Two text View with two Horizontal Recycler Views Child item layout-> Two text View with two Horizontal Recycler Views
ImageView Child item layout-> ImageView
Horizontal Recuycler View Child item layout-> Horizontal Recycler View
Vertical Recuycler View (with 12 items of sametype) Child item layout-> With 12 items with same type as a childs of Main RecyclerView


Feel free to comment and post your solutions and doubts. 

6 comments:

  1. Replies
    1. Sir, I have put the solution in above article, feel free to share you doubts. Thanks..!

      Delete
    2. What do you mean by `Child item layout`? is this the `LinearLayout`?

      Delete
    3. After reading it carefully, so you use `RecyclerView` instead of `NestedRecyclerView`.

      Does this means, you creating an item adapter for every layout? +Pradip

      Delete
    4. @Unknown Child item layout is item inside (Main layout-> RecyclerView)

      Delete
    5. @mochadwi I have used RecyclerView as MainLayout instead of (NestedScrollView -> with RecyclerView, all other layouts inside Linearlayout)
      Yes, I am creating an item adapter for every layout. Please suggest, if there is any better solution

      Delete

Popular Posts