首页 > 解决方案 > 如何在 android studio 中修复此错误?

问题描述

因此,每当我在 Android Studio 中创建新项目时,都会发生这种情况:activity_main.xml 中有 4 个错误,AndroidManifest.xml 中有 6 个错误

这是activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"`    <---ERROR(attribute android:layout_width is not allowed here)
`android:layout_height="match_parent"`    <---ERROR(attribute android:layout_height is not allowed )
`tools:context=".MainActivity">`      <---ERROR(attribute tools:context is not allowed here)

`<TextView`    <---ERROR(attribute TextView is not allowed here)


    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" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是 Mainfest

<application
android:allowBackup="true"<--ERROR(attribute android:allowBackup is not allowed here)
`android:icon="@mipmap/ic_launcher"`  <--ERROR(attribute android:icon is not allowed here)
android:label="@string/app_name"
`android:roundIcon="@mipmap/ic_launcher_round"` <--ERROR(attribute android:roundIcon is not ...)
`android:supportsRtl="true"`  <--ERROR(attribute android:supportsRtl is not ...)
`android:theme="@style/AppTheme">`  <--ERROR(attribute android:theme is not allowed..)
    `<activity android:name=".MainActivity">` <--ERROR(Unresolved class MainActivity)


     <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

标签: android

解决方案


从您的 activity_main.xml 文件中删除单引号:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读