我的应用程序在启动时不断崩溃,并出现以下错误:
java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.RelativeLayout.
我似乎无法弄清楚如何处理我的 xml
文件,因为我尝试将 ConstraintLayout
标记更改为 RelativeLayout
.
这是我的 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
tools:context=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:src="@drawable/ic_send"
android:tint="@android:color/white"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/fab"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"></android.support.design.widget.TextInputLayout>
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="284dp"
android:layout_marginRight="284dp"
android:hint="Message..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ListView
android:id="@+id/list_of_message"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_above="@+id/fab"
android:dividerHeight="16dp"
android:divider="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
最佳答案
在你的java文件中更改
Relativelayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
至
Relativelayout layout = (Relativelayout ) findViewById(R.id.activity_main);
这将解决你的问题。您更改 XML 但不更改 java 绑定(bind)。
关于android.support.constraint.ConstraintLayout 无法转换为 android.widget.RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070160/