AndroidManifest.xml中<receiver>需为<application>直接子元素的修复方法
报错原因
<receiver> element must be a direct child of the <application> element含义为<receiver>元素必须是<application>元素的直接子节点。你当前的代码把<receiver>标签写在了<activity>标签内部,不符合AndroidManifest的配置规则。
修复步骤
- 把原本嵌套在
<activity>内部的整个<receiver>代码块剪切,移动到<activity>结束标签</activity>的外面,<application>结束标签</application>的里面,保证<receiver>和<activity>是同级节点,同属于<application>的直接子节点。 - 给
<receiver>添加android:exported="true"属性,监听系统开机广播需要显式声明对外暴露,否则高版本Android系统会报错。 - 建议给
android:name的值前面加.,写成".MyReceiver",表示引用当前应用包下的MyReceiver类,避免类找不到的问题。
修复后完整AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ment4"> <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/Theme.Assignment4"> <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> <receiver android:name=".MyReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver> </application> </manifest>
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

