Search code examples
androidandroid-studio-3.0graphical-layout-editor

Layout Edtior not showing Toolbar - NegativeArraySizeException


All is working fine on phones and in emulator, but I can't use the Layout Editor in Android Studio because I get following error:

The following classes could not be instantiated:

  • android support.v7.widget.Toolbar (Open Class, Show Exception, Clear Cache)

The same I get with the ConstraintLayout in another layout.

Exception Details:

java.lang.NegativeArraySizeException
    at android.support.v7.appcompat.R$styleable.<clinit>(Unknown Source)
    at android.support.v7.widget.Toolbar.<init>(Toolbar.java:235)
    at android.support.v7.widget.Toolbar.<init>(Toolbar.java:228)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:211)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:681)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Image how it looks like:
enter image description here

My Layout XML for the Toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="?attr/toolbarTheme"
    app:popupTheme="?attr/toolbarPopupTheme"/>

Gradle for the "Support Library":

// Support Library  
implementation "com.android.support:support-core-utils:${supportLibVersion}"
implementation "com.android.support:appcompat-v7:${supportLibVersion}"
implementation "com.android.support:gridlayout-v7:${supportLibVersion}"
implementation "com.android.support:cardview-v7:${supportLibVersion}"
implementation "com.android.support:design:${supportLibVersion}"
implementation "com.android.support:recyclerview-v7:${supportLibVersion}"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'

supportLibVersion = '27.0.2'

I use Android Studio Version 3.0.1!

What have I already done and found in other StackOverflow threads:
- Invalid Caches and Restart
- Clean Project and Rebuild Project
- Themes I used change from parent "Theme.AppCompat" to "Base.Theme.AppCompat"


Solution

  • Problem:
    It seems the Layout Editor has some troubles if you have to much attributes in one <declare-stylable> tag defined in your attr.xml!

    Solution:
    I have 49 attributes in one <declare-stylable> tag.
    If I remove 5 attributes, so I have 45, the Layout Editor is working.

    I have also added an Issue on the Google Issue Tracker:
    https://issuetracker.google.com/issues/71641021

    UPDATE from Google Issue:
    This has been fixed for the Android Studio 3.1 release.