BroadcastReceiver java.lang.ClassNotFoundException

时间:2016-02-27 16:32:24

标签: android android-intent xamarin broadcastreceiver classnotfoundexception

我尝试在手机启动时显示简单的祝酒消息。 我浪费了将近一天的时间来弄清楚为什么我的代码不起作用。

以下是完整错误:

Unable to instantiate receiver com.debug.receivebootcomplete.debug.BroadcastReceiverClass: java.lang.ClassNotFoundException: com.debug.receivebootcomplete.debug.BroadcastReceiverClass

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.debug.receivebootcomplete.debug">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
        <receiver android:name=".BroadcastReceiverClass" android:exported="true">
            <intent-filter> 
                <action android:name="android.intent.action.BOOT_COMPLETED" /> 
            </intent-filter> 
        </receiver>
</application>
</manifest>

我的班级:

namespace Debug
{
    class BroadcastReceiverClass:BroadcastReceiver
    {
        public override void OnReceive (Context context, Intent intent)
        {
            Toast.MakeText (context,"Work",ToastLength.Short).Show ();
        }
    }
}

在模拟器调试中,应用程序在重新启动应用程序崩溃时抛出java.lang.ClassNotFoundException并在手机上。

提前谢谢!

1 个答案:

答案 0 :(得分:2)

谢谢@Talha!

我的错误是由intent-filter标记生成的。我不知道为什么,因为在每个主题中我都看到每个人都在Manifest文件中使用了该标记。

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

所以我将其删除并添加      [BroadcastReceiver] [IntentFilter(new[] {Intent.ActionBootCompleted})]

到BroadcastReceiver类。