I am working with android source. I am sending a broadcast from one of the applications and have written the intent filter for the same in another application’s(Launcher) manifest as shown.
<receiver
android:name="com.android.launcher2.LauncherModel"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.abc.THEMECHANGED" />
</intent-filter>
</receiver>
The class which extends receiver is com.android.launcher2.LauncherModel. I get the following exception in Launcher when the broadcast happens.
01-01 00:01:45.101: WARN/dalvikvm(835): threadid=1: thread exiting with uncaught exception (group=0x40b131f8)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): FATAL EXCEPTION: main
01-01 00:01:45.109: ERROR/AndroidRuntime(835): java.lang.RuntimeException: Unable to instantiate receiver com.android.launcher2.LauncherModel: java.lang.InstantiationException: can't instantiate class com.android.launcher2.LauncherModel; no empty constructor
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2108)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.app.ActivityThread.access$1500(ActivityThread.java:125)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.os.Looper.loop(Looper.java:137)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.app.ActivityThread.main(ActivityThread.java:4368)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at dalvik.system.NativeStart.main(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): Caused by: java.lang.InstantiationException: can't instantiate class com.android.launcher2.LauncherModel; no empty constructor
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at java.lang.Class.newInstanceImpl(Native Method)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at java.lang.Class.newInstance(Class.java:1319)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
01-01 00:01:45.109: ERROR/AndroidRuntime(835): ... 10 more
Can someone please let me know why this happens and the solution for the same
For the name you should only need the path to the class from the base package structure. In the manifest under the manifest tag there should be a package attribute, this will be your base package. Example:
So if the receiver were in the package com.jjnford.example.lancher2 the manifest name should be:
Also since your receiver is instantiated by the system you need to have an empty constructor as the system will call your onRecieve() method when your specified intent is caught.
UPDATE
Here is an example of how to created the BroadcastReceiver that is created programatically (look for my answer as it contains the code).