1

在我的项目中,我每天都面临着非常令人沮丧的 ConstraintLayout 问题。

项目gradle文件:

buildscript {

    ext.support_library_version = '27.0.2'
    ext.play_services_version = '11.6.2'
    ext.firebase_version = '11.6.2'

    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'io.realm:realm-gradle-plugin:4.2.0'
        classpath 'com.google.gms:google-services:3.1.2'

        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven { url "https://maven.google.com" }
        maven { url 'https://jitpack.io' }
    }
}

这是我的 app gradle 的简短版本:

apply plugin: 'com.android.library'
apply plugin: 'realm-android'

// set to 'cmake', 'none'
def nativeBuildSystem = 'none'//'cmake'

realm {
    syncEnabled = false
}

android {
    defaultConfig {
        targetSdkVersion 27
        compileSdkVersion 27
        minSdkVersion 21
        // for using vectors for less than 21 API
        vectorDrawables.useSupportLibrary = true

        versionCode 56
        dexOptions {
            preDexLibraries true
            javaMaxHeapSize "4g"
        }

        // Enabling multidex support.
        multiDexEnabled true
        versionName "1.6.5"

        renderscriptTargetApi 2
        renderscriptSupportModeEnabled true    // Enable RS support

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations {
    all*.exclude group: 'commons-logging', module: 'commons-logging'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    /*********************************  Deep Linking Library *********************************************/
    api 'com.airbnb:deeplinkdispatch:3.1.1'
    annotationProcessor 'com.airbnb:deeplinkdispatch-processor:3.1.1'

    /*********************************    Android SDK  *********************************************/
    api "com.android.support:appcompat-v7:$support_library_version"
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api "com.android.support:percent:$support_library_version"
    api "com.android.support:recyclerview-v7:$support_library_version"
    api "com.android.support:gridlayout-v7:$support_library_version"
    api "com.android.support:cardview-v7:$support_library_version"
    api "com.android.support:design:$support_library_version"
    api "com.android.support:support-vector-drawable:$support_library_version"
    api "com.android.support:support-core-utils:$support_library_version"
    api 'com.android.support:multidex:1.0.2'

}

下面是我在布局编辑器中看到的:

在此处输入图像描述

在我的xml下面:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.constraint.ConstraintLayout>

在这种情况下,布局编辑器说:

在此处输入图像描述

如果我点击Show Exception我会看到:

java.lang.NegativeArraySizeException
at android.support.constraint.R$styleable.<clinit>(Unknown Source)
at android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:440)
at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:420)
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)

我尝试了很多以解决它,但没有成功。我没有使用像无效缓存/清理项目/重建项目等简单的方法来解决...

4

2 回答 2

1

问题:如果在attr.xml中定义的一个标签中有太多属性,
布局编辑器似乎会出现一些问题!<declare-stylable>

解决方案:
我在一个标签中有49 个属性。 如果我删除4 个属性,所以我有45,布局编辑器正在工作。<declare-stylable>

我还在 Google 问题跟踪器上添加了一个问题:
https ://issuetracker.google.com/issues/71641021

来自 Google 的更新问题:
这已针对 Android Studio 3.1 版本进行了修复。

于 2018-01-07T18:40:27.603 回答
0

不要使用ConstraintLayout,而是使用Relative...or Linear...。那应该行得通。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent" />
</LinearLayout>
于 2018-01-17T09:33:38.640 回答