android.support.constraint.ConstraintLayout无法转换为android.widget.RelativeLayout

时间:2018-08-29 05:40:48

标签: android

我的应用在启动时不断崩溃,出现以下错误:

  

java.lang.ClassCastException:   android.support.constraint.ConstraintLayout无法转换为   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>

3 个答案:

答案 0 :(得分:0)

在您的Java文件更改中

Relativelayout layout = (ConstraintLayout) findViewById(R.id.activity_main);

收件人

Relativelayout layout = (Relativelayout ) findViewById(R.id.activity_main);

那将解决您的问题。您更改XML但不更改Java绑定。

答案 1 :(得分:0)

您不能使用ConstraintLayout转换Relativelayout,因此您必须使用与XML这样的类名来转换它

Relativelayout layout = (Relativelayout ) findViewById(R.id.layout);

答案 2 :(得分:0)

首先,转到.xml文件并更改最底行:

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

然后,再次运行您的应用程序,您将不会收到该错误。