错误:在升级到Android 3.1后发现未知元素<intent-filter>。

3
今天我升级到Android Studio 3.1.4,遇到了第一个错误。
The option 'android.enableAapt2' is deprecated and should not be used anymore.
Use 'android.enableAapt2=true' to remove this warning.
It will be removed at the end of 2018..

接着按建议修改android.enableAapt2为true。

之后出现了新的错误:AAPT2错误。

error: unknown element <intent-filter> found.
Message{kind=ERROR, text=error: unknown element <intent-filter> found., sources=[C:\FILE\Android Studio\UltraGreek\UltraGreekv.4.7\app\build\intermediates\manifests\full\debug\AndroidManifest.xml:37], original message=, tool name=Optional.of(AAPT)}

我在 app/srs/main/AndroidManifest.xml 中的清单文件如下:

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

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

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

        <activity android:name=".ActivityAbout"/>
        <activity android:name=".WebViewer"/>

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

            <category android:name="android.intent.category.DEFAULT"/>

            <data android:mimeType="text/plain"/>
        </intent-filter>

        <activity
            android:name=".ActivityStats"
            android:label="Στατιστικά">
        </activity>
    </application>

</manifest>
3个回答

6
AAPT2是AAPT的替代品。如果您在谷歌搜索与AAPT2相关的内容,有人会建议您禁用它。不要这样做。它已被弃用并计划删除,这意味着您仍将遇到这些错误。
为了学习,让我们分解一下错误消息。它格式化为JSON,这意味着每个等号标记一个新元素。这是两个相关的元素:
text=error: unknown element <intent-filter> found.
sources=[C:\FILE\Android Studio\UltraGreek\UltraGreekv.4.7\app\build\intermediates\manifests\full\debug\AndroidManifest.xml:37]

这意味着您在第37行的清单中有一个与intent-filter相关的异常。如果该元素放错位置,它将显示为未知元素。请参阅迁移指南
现在,我没有行号,因为Stack Overflow不包括它们。但是,如果您查看应用程序标记内部,您会看到以下内容:
<activity android:name=".WebViewer"/>

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

    <category android:name="android.intent.category.DEFAULT"/>

    <data android:mimeType="text/plain"/>
</intent-filter>

问题在于activity标签已经关闭,这意味着它实际上看起来像这样(伪代码):
<application ...>
    <activity android:name=".WebViewer"/>

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

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>
    </intent-filter>
</application>

<intent-filter> 只能在一个 activity 下使用,(来自之前链接的迁移指南):

在 AAPT 的旧版本中,Android 清单文件中嵌套在错误节点中的元素要么被忽略,要么会导致警告。[...]

这意味着现在错误的节点会阻止编译。这就是为什么启用 AAPT2 时会出现错误,而使用 AAPT 则不会。

解决方法是将 <intent-filter> 移动到支持节点中,也就是需要将其包装在 activity 标签内部。我不知道你想把它放在哪里,所以我不会提供任何准确的代码。但是,intent-filter 标签需要在 activity 标签内部,如下所示:

<activity android:name="" android:label="">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

0

你可以尝试使用接收器标签:

<application ...>
    <activity android:name=".WebViewer"/>
    <receiver android:name="">
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
    </receiver>
</application>


0

这是因为我们在意图过滤器之前没有关闭活动选项卡,请确保在意图过滤器之后关闭它。


1
欢迎来到Stack Overflow。虽然我们感谢您的回答,但如果它能在其他答案的基础上提供额外的价值,那就更好了。在这种情况下,您的答案并没有提供额外的价值,因为另一个用户已经发布了该解决方案。如果之前的答案对您有帮助,您应该在获得足够的声望后投票支持它。 - Sergei Kozelko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接