首页 > 解决方案 > Android Studio 无法启动约束布局

问题描述

[在此处输入图像描述]

在右上角,缩放按钮旁边会出现一个蓝色警告,基本上说:

未能启动一门或多门课程

无法实例化以下类:

异常详情:

异常详情 java.lang.NoClassDefFoundError: android/support/constraint/R$styleable android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:599) android.support.constraint.ConstraintLayout.(ConstraintLayout.java:576) java. lang.reflect.Constructor.newInstance(Constructor.java:423) android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) android.view.LayoutInflater.inflate(LayoutInflater.java:492) android.view.LayoutInflater.inflate( LayoutInflater.java:394)

并且也是activity_main.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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

在 tools:context=".MainActivity" 行上 .MainActivity 以红色显示“未解决的类 MainActivity”

这是我的 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kaan.asd">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我是 Android Studio 的新手,我已经尝试解决这个问题已经有一段时间了,我尝试过清除缓存、清理/重建项目,但都没有解决我的问题。我在 Windows 上使用带有 4.10.1 gradle 版本的 Android Studio 3.3.1 版本。如果有人可以帮助我修复它,我会很高兴。

标签: androidandroid-studioandroid-layoutandroid-constraintlayout

解决方案


您必须在 android 目录中添加约束布局的存储库:app/build.gradle,如下所示:

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

然后同步您的项目并享受这个完美的布局。


推荐阅读