我正在尝试使用ContraintsLayout创建布局。
在我的布局中,我添加了横向GuideLine
。
但是,当我运行该应用时,它会崩溃,如果我删除GuideLine
,它就可以运行。
我还检查了APK,并且有一个班级java.awt.Rectagle
StackStrace
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>
答案 0 :(得分:6)
更新:
此问题已在Android Studio 2.2预览2中修复(从this page下载)。
修正指南在设备上崩溃
来自changelog。
请注意,您必须将app:orientation
更改为android:orientation
将方向属性命名空间从app更改为android以修复与GridLayout的冲突
(也在changelog中提到)。
问题是Guideline类内部使用java.awt.Rectangle
,并且java.awt
库不适用于android(除了java.awt.font类)。
您无法手动编辑Guideline类,您也不应该这样做。现在最好的事情是在https://code.google.com/p/android/issues报告问题,就像Selvin说的那样,并等待修复。在此期间,您无法使用指南类。
幸运的是,指南更像是一个便利课,所以你现在应该可以没有它。如果你确实需要这样的东西,那么你可以暂时使用0dp
厚的常规View元素并将约束与它挂钩。
答案 1 :(得分: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" />
我希望这可以帮助某个人