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

标签 android

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

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/

相关文章:

android - 在 MPAndroid 条形图版本 3 中设置标签

android - 带有OpenCV3.0.0 Android的Camera Flash无法正常工作

java - 一般使用 java JNI GetFieldID 和 JNI

java - android中按钮Onclick选择和取消选择状态

java - 如何解析删除所有非 ASCII 字节的字节数组?

android - 有什么方法可以检测 videoview 上的实时流缓冲是否停止?

android - EXTRA_VIDEO_QUALITY 被忽略

android - 即使在 Android 4.4.2 中关闭位置服务,GetCurrentLocation 也在 Cordova/PhoneGap App 上运行

javascript - 在 native react 中通过表单数据发送音频

android - XMPP Smack - 消息被修改