Unable to instantiate receiver

10,957

Solution 1

You should have action declared in their own intent filter.

<receiver android:name=".ExtIntentReceiver " android:exported="true">
    <intent-filter>
        <action android:name="com.example.app.START_SERVICE" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.example.app.STOP_SERVICE" />
    </intent-filter>
</receiver>

Apart from that make sure you give correct package name to Receiver inside manifest. And make sure you should spell it correctly.

Solution 2

If you want to have BroadcastReceiver as inner class and you want to use it without making instance of it you should make it STATIC like this

public static class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //do what you want
    }
}
Share:
10,957
XorOrNor
Author by

XorOrNor

Updated on June 04, 2022

Comments

  • XorOrNor
    XorOrNor almost 2 years

    I'm getting "Unable to instantiate receiver" errors when sending intents to my app. It seems Eclipse somehow cannot see my ExIntentReceiver class. I don't know why it happens; ExIntentReceiver is in the same package (com.example.app).

    ExIntentReceiver.java:

    package com.example.app
    
    public class ExIntentReceiver extends BroadcastReceiver {
    
     @Override
       public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
      if(action.equals("com.example.app.START_SERVICE"))    {
            Log.v("service", "is started");
          } else if(action.equals("com.example.app.STOP_SERVICE"))  {
              Log.v("service", "is stopped");
          }
    
       }
    }
    

    Manifest:

     <receiver android:name=".ExIntentReceiver" android:exported="true">  
          <intent-filter>
         <action android:name="com.example.app.START_SERVICE" />
           <action android:name="com.example.app.STOP_SERVICE" />
         </intent-filter>
      </receiver>
    

    logcat:

    04-22 10:37:13.361: E/AndroidRuntime(11213): FATAL EXCEPTION: main
    04-22 10:37:13.361: E/AndroidRuntime(11213): java.lang.RuntimeException: Unable to instantiate receiver com.example.app.ExIntentReceiver: java.lang.ClassNotFoundException: com.example.app.ExIntentReceiver
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2261)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.app.ActivityThread.access$1600(ActivityThread.java:140)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.os.Handler.dispatchMessage(Handler.java:99)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.os.Looper.loop(Looper.java:137)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at android.app.ActivityThread.main(ActivityThread.java:4921)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at java.lang.reflect.Method.invoke(Method.java:511)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at dalvik.system.NativeStart.main(Native Method)
    04-22 10:37:13.361: E/AndroidRuntime(11213): Caused by: java.lang.ClassNotFoundException: com.example.app.ExIntentReceiver
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    04-22 10:37:13.361: E/AndroidRuntime(11213):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)