8

我正在尝试使用 ContraintsLayout 创建布局。

在我的布局中,我添加了 Horizo​​ntal GuideLine

但是当我运行应用程序时它会崩溃,如果我删除GuideLine它就可以了。

我还检查了 APK 并且有一个类java.awt.Rectagle

堆栈跟踪

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Rectangle;
 at android.support.constraint.solver.widgets.Guideline.<init>(Guideline.java:43)
 at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:128)
 at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:106)
 at android.support.constraint.ConstraintLayout.onFinishInflate(ConstraintLayout.java:87)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
 at com.infostretch.app.MainActivity.onCreate(MainActivity.java:11)
 at android.app.Activity.performCreate(Activity.java:6669)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2578)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686)
 at android.app.ActivityThread.-wrap12(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:5969)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.Rectangle" on path: DexPathList[[zip file "/data/app/com.infostretch.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.infostretch.app-1/lib/x86_64, /system/lib64, /vendor/lib64]]
 at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
 at android.support.constraint.solver.widgets.Guideline.<init>(Guideline.java:43) 
 at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:128) 
 at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:106) 
 at android.support.constraint.ConstraintLayout.onFinishInflate(ConstraintLayout.java:87) 
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) 
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
 at com.infostretch.app.MainActivity.onCreate(MainActivity.java:11) 
 at android.app.Activity.performCreate(Activity.java:6669) 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2578) 
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686) 
 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:154) 
 at android.app.ActivityThread.main(ActivityThread.java:5969) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691) 

布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="80dp">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:src="@color/accent_material_light"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    app:layout_constraintTop_toTopOf="@+id/constraintLayout"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp" />

<TextView
    android:id="@+id/textView11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    app:layout_constraintLeft_toRightOf="@+id/imageView"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    app:layout_constraintTop_toTopOf="@+id/constraintLayout"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintRight_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="80dp"
    tools:layout_editor_absoluteY="16dp" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="28dp"
    android:layout_height="28dp"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="8dp"
    android:src="@color/abc_input_method_navigation_guard"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    app:layout_constraintTop_toBottomOf="@+id/textView11"
    tools:layout_constraintRight_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="367dp"
    tools:layout_editor_absoluteY="40dp" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:orientation="horizontal"
    app:relativeBegin="83dp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="83dp" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="120dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:orientation="vertical"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintRight_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="99dp">

    <TextView
        android:id="@+id/textView12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="MapView"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="230dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="0dp"
    android:layout_height="120dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:orientation="vertical"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintRight_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="227dp">

    <TextView
        android:id="@+id/textView13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Alert View"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="358dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout3"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
    tools:layout_constraintBottom_creator="0"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintRight_creator="0"
    tools:layout_constraintTop_creator="0"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="355dp">

    <TextView
        android:id="@+id/textView14"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Recycler View"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="522dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
    app:layout_constraintRight_toRightOf="@+id/constraintLayout"
    tools:layout_constraintBottom_creator="0"
    tools:layout_constraintLeft_creator="0"
    tools:layout_constraintRight_creator="0"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="554dp">

  </LinearLayout>
</android.support.constraint.ConstraintLayout>
4

2 回答 2

6

更新:
此问题已在 Android Studio 2.2 预览版 2 中修复(从此页面下载)。

修复了设备上的指南崩溃

更新日志

请注意,您必须更改app:orientationandroid:orientation

将方向属性命名空间从 app 更改为 android 以修复与 GridLayout 的冲突

(也在更新日志中提到)。


问题是 Guideline 类在内部使用java.awt.Rectangle,并且java.awt库不适用于 android(除了java.awt.font类)。

您不能手动编辑 Guideline 类,也不应该这样做。现在最好的办法是像 Selvin 所说的那样在https://code.google.com/p/android/issues报告问题,然后等待修复。与此同时,您不能使用 Guideline 类。

幸运的是,Guideline 更像是一个便利类,所以你现在应该可以不用它了。如果你真的需要这样的东西,那么你可以暂时使用一个0dp厚的常规 View 元素并将约束连接到它。

于 2016-05-20T12:12:05.480 回答
0

我遇到了类似的问题......但后来我发现我正在使用 androidX 但错误地把

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="406dp" />

而不是使用 androidX 等价物

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="406dp" />

我希望这可以帮助那里的人

于 2020-05-29T08:57:14.703 回答