LinearLayout元素不允许在此处使用。

huangapple go评论64阅读模式
英文:

Element LinearLayout Not Allowed Here

问题

我在我的AndroidManifest.xml文件中收到错误消息:元素LinearLayout在此处不允许。

我已经尝试重新启动Android Studio,但没有任何效果。

这是我的代码:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Map"
        tools:targetApi="31">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="XXXXXXXXXXXXXXXXXXXX"></meta-data>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_lat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_lng"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>


    </application>

</manifest>

为什么我会收到这个警告?

英文:

I'm getting the error Element LinearLayout is not allowed here in my AndroidManifest.xml file.
I've tried restarting Android studio, but that hasn't had any effect.

Here is my code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
        android:fullBackupContent=&quot;@xml/backup_rules&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/Theme.Map&quot;
        tools:targetApi=&quot;31&quot;&gt;
        &lt;meta-data
            android:name=&quot;com.google.android.geo.API_KEY&quot;
            android:value=&quot;XXXXXXXXXXXXXXXXXXXX&quot;&gt;&lt;/meta-data&gt;
        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;LinearLayout
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;vertical&quot;&gt;

            &lt;TextView
                android:id=&quot;@+id/tv_lat&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot; /&gt;

            &lt;TextView
                android:id=&quot;@+id/tv_lng&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot; /&gt;

        &lt;/LinearLayout&gt;


    &lt;/application&gt;

&lt;/manifest&gt;

Why am I getting this warning?

答案1

得分: 1

你不能在清单文件中定义布局信息。清单文件用于描述应用程序,例如应用程序使用的权限和应用程序中存在的活动。

你最可能想要做的是打开你的 activity_main.xml 并将你的布局添加到其中。

请参考布局指南,了解如何入门,该指南来自Android开发者文档。

另外,要获取有关清单文件用途的更多信息,请参阅清单文件文档

要开始操作,你需要将XML拆分成适当的文件:

androidManifest.xml

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Map"
        tools:targetApi="31">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="XXXXXXXXXXXXXXXXXXX"></meta-data>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/container"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_lng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
英文:

You can't define layout information in your Manifest. The Manifest is meant to describe the application, like which permissions the app uses and which activities reside in your app.

What you're most likely want to do, is open you activity_main.xml and Add your layout to that.

Please refer to layout guide from the Android Developers documentation on how to get started.

Also for more information on what the Manifest is meant for, refer to the Manifest Documentation.

What you'll want to do to get started, is split the xml in the the appropriate files:

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

&lt;application
    android:allowBackup=&quot;true&quot;
    android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
    android:fullBackupContent=&quot;@xml/backup_rules&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/Theme.Map&quot;
    tools:targetApi=&quot;31&quot;&gt;
    &lt;meta-data
        android:name=&quot;com.google.android.geo.API_KEY&quot;
        android:value=&quot;XXXXXXXXXXXXXXXXXXX&quot;&gt;&lt;/meta-data&gt;
    &lt;activity
        android:name=&quot;.MainActivity&quot;
        android:exported=&quot;true&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

&lt;/application&gt;

</manifest>

activity_main.xml

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:id=&quot;@+id/container&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/tv_lat&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/tv_lng&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot; /&gt;

&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2023年6月29日 15:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定